Ginés Martínez Sánchez avatar Ginés Martínez Sánchez committed 146b175 Draft

developing wsgi server

Comments (0)

Files changed (8)

ginsfsm/c_sock.py

         self.connected = False
         self.addr = None        # dont't make private: waitress use it.
         self.socket = None      # dont't make private: waitress use it.
+        self.sockname = None
 
         self._socket_map = {}   # socket_map set by gaplic. Dict {fd:Gobj}
         self._impl_poll = None  # _poll(),epoll() implementation
                 # polling of broken sockets).
                 self.remove_socket()
                 raise
+        self.sockname = self.socket.getsockname()
 
     def __repr__(self):
         status = [self.__class__.__module__ + "." + self.__class__.__name__]
     def handle_connect(self):
         data = {}
         data['peername'] = self.socket.getpeername()
-        data['sockname'] = self.socket.getsockname()
+        self.sockname = data['sockname'] = self.socket.getsockname()
         logging.info("%s <== Connected! %s host '%s'" % (
             self.name,
             "FROM" if self._clisrv else "TO",

ginsfsm/examples/http/simple_http_server.py

 import logging
 logging.basicConfig(level=logging.DEBUG)
 
+from ginsfsm.gaplic import GAplic
 from ginsfsm.smachine import SMachine
 from ginsfsm.c_sock import GSock
 from ginsfsm.gobj import GObj
-from ginsfsm.gaplic import GAplic
 from ginsfsm.protocols.http.server.c_http_server import GHttpServer
 from ginsfsm.protocols.http.common.response import HttpSimpleOkResponse
 

ginsfsm/examples/wsgi/simple_wsgi_server.py

+"""
+Simple WSGI server
+==================
+
+It uses :class:`ginsfsm.protocols.wsgi.server.c_wsgi_server.GWsgiServer`.
+
+.. autofunction:: main
+
+"""
+
+import logging
+
+from ginsfsm.gaplic import GAplic
+from ginsfsm.smachine import SMachine
+from ginsfsm.c_sock import GSock
+from ginsfsm.protocols.wsgi.server.c_wsgi_server import GWsgiServer
+
+
+#===============================================================
+#                   Server
+#===============================================================
+def application(environ, start_response):
+    start_response('200 OK', [('Content-Type', 'text/plain')])
+    yield 'Hello World\n'
+
+
+def paste_app_factory(global_config, **local_conf):
+    return application
+
+
+#===============================================================
+#                   Main
+#===============================================================
+def main(global_config, **settings):
+    """ Entry point to run with gserve (PasteDeploy)
+    """
+    ga = GAplic('Wsgi-Example', **settings)
+    ga.create_gobj(
+        'wsgi-server',
+        GWsgiServer,
+        None,
+        host='0.0.0.0',
+        port=8000,
+        application=application
+    )
+    return ga
+
+
+if __name__ == "__main__":
+
+    settings = {
+    }
+    ga = main(settings)
+    SMachine._global_trace_mach = True
+    GSock._global_trace_dump = True
+
+    try:
+        ga.mt_process()
+    except (KeyboardInterrupt, SystemExit):
+        print('Program stopped')

ginsfsm/protocols/http/common/response.py

             self.channel.send_event(
                 self.gsock,
                 'EV_WRITE_OUTPUT_DATA',
-                b'0\r\n\r\n'
+                data=b'0\r\n\r\n'
             )
         if self.close_on_finish:
             self.gsock.close_when_flushed = True

ginsfsm/protocols/http/server/c_http_clisrv.py

     except:
         logging.exception('Exception when serving %s' % response.request.path)
         if not response.wrote_header:
-            if self.expose_tracebacks:
+            if self.parent.expose_tracebacks:
                 body = traceback.format_exc()
             else:
                 body = ('The server encountered an unexpected '

ginsfsm/protocols/http/server/c_http_server.py

     'origins': [None, None, 0, None,
         "TODO:list of (host, port) tuples allowed to connect from"],
 
+    'expose_tracebacks': [bool, False, 0, None,
+        "expose tracebacks of uncaught exceptions"],
     'maximum_simultaneous_requests': [int, 0, 0, None,
         "maximum simultaneous requests. Default: 0, without limit."
         ],
             prefix_name = self.name
         else:
             prefix_name = None
-        self.serversock = self.create_gobj(
+        self.gserversock = self.create_gobj(
             prefix_name + '.http-server' if prefix_name else None,
             GServerSock,
             self,

ginsfsm/protocols/wsgi/common/wsgi_response.py

             path = path[1:]
 
         environ = {}
-        environ['gsock'] = gsock
-        environ['ext_event'] = self.ext_event
+        #environ['gsock'] = gsock
+        #environ['ext_event'] = self.ext_event
         environ['REQUEST_METHOD'] = request.command.upper()
         environ['SERVER_PORT'] = str(wsgi_server.effective_port)
         environ['SERVER_NAME'] = wsgi_server.server_name

ginsfsm/protocols/wsgi/server/c_wsgi_server.py

     }
 }
 
-GWSGISERVER_GCONFIG = GHTTPSERVER_GCONFIG + {
-    'expose_tracebacks': [bool, False, 0, None,
-        "expose tracebacks of uncaught exceptions"],
+GWSGISERVER_GCONFIG = GHTTPSERVER_GCONFIG.copy()
+GWSGISERVER_GCONFIG.update({
     #TODO: implement a multi wsgi application
     'application': [None, None, 0, None, "wsgi application"],
-}
+})
 
 
 class GWsgiServer(GObj):
         else:
             prefix_name = None
 
-        self.server = self.create_gobj(
+        self.ghttpserver = self.create_gobj(
             prefix_name + '.sock-server' if prefix_name else None,
             GHttpServer,
             self,
             responseless_timeout=2,
             maximum_simultaneous_requests=0,
         )
+        self.serversock = self.ghttpserver.gserversock.socket
 
         # Used in environ
-        self.effective_host, self.effective_port = \
-            self.serversock.getsockname()
+        self.effective_host, self.effective_port = self.getsockname()
         self.server_name = self._get_server_name(self.host)
 
     def select_app(self, request):
         return srv_name
 
     def getsockname(self):
-        return self.serversock.socket.getsockname()
+        return self.serversock.getsockname()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.