changeset 1168:9754451de554

thfcgi: fixed typos, less brackets, cleanup
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 13 Aug 2006 19:10:59 +0200
parents 038d975120bd
children 7794b6947a06
files MoinMoin/support/thfcgi.py
diffstat 1 files changed, 24 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/support/thfcgi.py	Sun Aug 13 19:10:52 2006 +0200
+++ b/MoinMoin/support/thfcgi.py	Sun Aug 13 19:10:59 2006 +0200
@@ -9,6 +9,9 @@
     Added "external application" support, refactored code
         by Alexander Schremmer <alex AT alexanderweb DOT de>
 
+    Cleanup, fixed typos, PEP-8, ...
+        by Thomas Waldmann <tw AT waldmann-edv DOT de>
+
     For code base see:
     http://cvs.lysator.liu.se/viewcvs/viewcvs.cgi/webkom/thfcgi.py?cvsroot=webkom
 
@@ -137,7 +140,7 @@
         value = data[pos:pos+valuelen]
         pos += valuelen
 
-        return (name, value, pos)
+        return name, value, pos
 
     def write_pair(self, name, value):
         """Write a FastCGI key-value pair to the server."""
@@ -161,12 +164,11 @@
         """Read a FastCGI record from the server."""
         data = sock.recv(8)
         if not data:
-            # No data recieved. This means EOF. 
+            # No data received. This means EOF. 
             return None
 
-        fields = struct.unpack(FCGI_Record_header, data)
-        (self.version, self.rec_type, self.req_id,
-         contentLength, paddingLength) = fields
+        self.version, self.rec_type, self.req_id, contentLength, paddingLength = \
+            struct.unpack(FCGI_Record_header, data)
 
         self.content = ""
         while len(self.content) < contentLength:
@@ -177,8 +179,7 @@
 
         # Parse the content information
         if self.rec_type == FCGI_BEGIN_REQUEST:
-            (self.role, self.flags) = struct.unpack(FCGI_BeginRequestBody,
-                                                    self.content)
+            self.role, self.flags = struct.unpack(FCGI_BeginRequestBody, self.content)
             self.keep_conn = self.flags & FCGI_KEEP_CONN
 
         elif self.rec_type == FCGI_UNKNOWN_TYPE:
@@ -190,10 +191,9 @@
             while pos < len(self.content):
                 name, value, pos = self.read_pair(self.content, pos)
                 self.values[name] = value
+
         elif self.rec_type == FCGI_END_REQUEST:
-            (self.appStatus,
-             self.protocolStatus) = struct.unpack(FCGI_EndRequestBody,
-                                                  self.content)
+            self.appStatus, self.protocolStatus = struct.unpack(FCGI_EndRequestBody, self.content)
 
         return 1
 
@@ -212,15 +212,13 @@
                 content = content + self.write_pair(i, self.values[i])
 
         elif self.rec_type == FCGI_END_REQUEST:
-            content = struct.pack(FCGI_EndRequestBody, self.appStatus,
-                                  self.protocolStatus)
+            content = struct.pack(FCGI_EndRequestBody, self.appStatus, self.protocolStatus)
 
         # Align to 8-byte boundary
         clen = len(content)
         padlen = ((clen + 7) & 0xfff8) - clen
 
-        hdr = struct.pack(FCGI_Record_header, self.version, self.rec_type,
-                          self.req_id, clen, padlen)
+        hdr = struct.pack(FCGI_Record_header, self.version, self.rec_type, self.req_id, clen, padlen)
 
         try:
             sock.sendall(hdr + content + padlen*"\x00")
@@ -280,8 +278,7 @@
         environ read from the server for this request."""
         self.stdin.reset()
         # cgi.FieldStorage will eat the input here...
-        r = cgi.FieldStorage(fp=self.stdin, environ=self.env,
-                             keep_blank_values=1)
+        r = cgi.FieldStorage(fp=self.stdin, environ=self.env, keep_blank_values=1)
         # hence, we reset here so we can obtain
         # the data again...
         self.stdin.reset()
@@ -382,8 +379,7 @@
         if rec_type in KNOWN_MANAGEMENT_TYPES:
             self._handle_known_man_types(rec)
         else:
-            # It's a management record of an unknown
-            # type. Signal the error.
+            # It's a management record of an unknown type. Signal the error.
             rec = Record()
             rec.rec_type = FCGI_UNKNOWN_TYPE
             rec.unknownType = rec_type
@@ -397,7 +393,8 @@
 
             params = {'FCGI_MAX_CONNS': FCGI_MAX_CONNS,
                       'FCGI_MAX_REQS': FCGI_MAX_REQS,
-                      'FCGI_MPXS_CONNS': FCGI_MPXS_CONNS}
+                      'FCGI_MPXS_CONNS': FCGI_MPXS_CONNS,
+                     }
 
             for name in rec.values.keys():
                 if params.has_key(name):
@@ -414,7 +411,7 @@
             self._handle_begin_request(rec)
             return
         elif rec.req_id != self.req_id:
-            #print >> sys.stderr, "Recieved unknown request ID", rec.req_id
+            #print >> sys.stderr, "Received unknown request ID", rec.req_id
             # Ignore requests that aren't active
             return
         if rec.rec_type == FCGI_ABORT_REQUEST:
@@ -435,7 +432,7 @@
             self._handle_data(rec)
         else:
             # Should never happen. 
-            #print >> sys.stderr, "Recieved unknown FCGI record type", rec.rec_type
+            #print >> sys.stderr, "Received unknown FCGI record type", rec.rec_type
             pass
 
         if self.env_complete and self.stdin_complete:
@@ -464,7 +461,7 @@
         """Handle environment."""
         if self.env_complete:
             # Should not happen
-            #print >> sys.stderr, "Recieved FCGI_PARAMS more than once"
+            #print >> sys.stderr, "Received FCGI_PARAMS more than once"
             return
 
         if not rec.content:
@@ -477,7 +474,7 @@
         """Handle stdin."""
         if self.stdin_complete:
             # Should not happen
-            #print >> sys.stderr, "Recieved FCGI_STDIN more than once"
+            #print >> sys.stderr, "Received FCGI_STDIN more than once"
             return
 
         if not rec.content:
@@ -491,7 +488,7 @@
         """Handle data."""
         if self.data_complete:
             # Should not happen
-            #print >> sys.stderr, "Recieved FCGI_DATA more than once"
+            #print >> sys.stderr, "Received FCGI_DATA more than once"
             return
 
         if not rec.content:
@@ -548,7 +545,7 @@
                     raise ValueError("FastCGI port is not setup correctly")
         except socket.error, (err, errmsg):
             if err != errno.ENOTCONN:
-                raise RuntimeError("No FastCGI environment: %s - %s" % (`err`, errmsg))
+                raise RuntimeError("No FastCGI environment: %s - %s" % (repr(err), errmsg))
 
         self.sock = s
 
@@ -580,7 +577,7 @@
         self.sock.listen(50)
 
         while 1:
-            (conn, addr) = self.sock.accept()
+            conn, addr = self.sock.accept()
             thread.start_new_thread(self.accept_handler, (conn, addr))
 
 class unTHFCGI(FCGIbase):
@@ -596,6 +593,6 @@
         self.sock.listen(50)
 
         while 1:
-            (conn, addr) = self.sock.accept()
+            conn, addr = self.sock.accept()
             self.accept_handler(conn, addr)