No b'...'.format() method (see http://bugs.python.org/issue3982).
[pyassuan.git] / pyassuan / server.py
index e7c36a9bf04fd6c35312b84518c2687cd2711269..1318a2b465c0f10b4b51dcefe2a0af0688b1d731 100644 (file)
@@ -1,4 +1,18 @@
-# Copyright
+# Copyright (C) 2012 W. Trevor King <wking@drexel.edu>
+#
+# This file is part of pyassuan.
+#
+# pyassuan is free software: you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation, either version 3 of the License, or (at your option) any later
+# version.
+#
+# pyassuan is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# pyassuan.  If not, see <http://www.gnu.org/licenses/>.
 
 import logging as _logging
 import re as _re
@@ -60,10 +74,10 @@ class AssuanServer (object):
     def connect(self):
         if not self.input:
             self.logger.info('read from stdin')
-            self.input = _sys.stdin
+            self.input = _sys.stdin.buffer
         if not self.output:
             self.logger.info('write to stdout')
-            self.output = _sys.stdout
+            self.output = _sys.stdout.buffer
 
     def disconnect(self):
         if self.close_on_disconnect:
@@ -78,7 +92,10 @@ class AssuanServer (object):
             line = self.input.readline()
             if not line:
                 break  # EOF
-            if not line.endswith('\n'):
+            if len(line) > _common.LINE_LENGTH:
+                self.raise_error(
+                    _error.AssuanError(message='Line too long'))
+            if not line.endswith(b'\n'):
                 self.logger.info('C: {}'.format(line))
                 self.send_error_response(
                     _error.AssuanError(message='Invalid request'))
@@ -87,7 +104,7 @@ class AssuanServer (object):
             self.logger.info('C: {}'.format(line))
             request = _common.Request()
             try:
-                request.from_string(line)
+                request.from_bytes(line)
             except _error.AssuanError as e:
                 self.send_error_response(e)
                 continue
@@ -120,9 +137,9 @@ class AssuanServer (object):
         """For internal use by ``.handle_requests()``
         """
         rstring = str(response)
-        self.logger.info('S: {}'.format(rstring))
-        self.output.write(rstring)
-        self.output.write('\n')
+        self.logger.info('S: {}'.format(response))
+        self.output.write(bytes(response))
+        self.output.write(b'\n')
         try:
             self.output.flush()
         except IOError:
@@ -274,8 +291,8 @@ class AssuanSocketServer (object):
 
     def spawn_thread(self, name, socket, address):
         server = self.server(name=name, **self.kwargs)
-        server.input = socket.makefile('r')
-        server.output = socket.makefile('w')
+        server.input = socket.makefile('rb')
+        server.output = socket.makefile('wb')
         thread = _threading.Thread(target=server.run, name=name)
         thread.start()
         self.threads.append(thread)