[py-svn] r8305 - in py/dist/py: documentation execnet/bin execnet/testing

hpk at codespeak.net hpk at codespeak.net
Sun Jan 16 13:33:03 CET 2005


Author: hpk
Date: Sun Jan 16 13:33:03 2005
New Revision: 8305

Added:
   py/dist/py/execnet/bin/socketserver.py
      - copied unchanged from r8304, py/dist/py/execnet/bin/startserver.py
Removed:
   py/dist/py/execnet/bin/startserver.py
Modified:
   py/dist/py/documentation/execnet.txt
   py/dist/py/execnet/testing/test_gateway.py
Log:
renamed startserver.py to socketserver.py 
fixed execnet documentation 



Modified: py/dist/py/documentation/execnet.txt
==============================================================================
--- py/dist/py/documentation/execnet.txt	(original)
+++ py/dist/py/documentation/execnet.txt	Sun Jan 16 13:33:03 2005
@@ -180,52 +180,28 @@
 basically listens and accepts socket connections, receives one 
 liners and executes them. 
 
-Here is the code for making the above triangle happen::
+Here are 20 lines of code making the above triangle happen::
 
-    import py
-    socketserverbootstrap = py.code.Source( 
-            autopath.dirpath('bin/socketserver').read(), 
-            """
-            import socket 
-            portrange = channel.receive()
-            for i in portrange: 
-                try:
-                    sock = bind_and_listen(("localhost", i))
-                except socket.error: 
-                    continue
-                else:
-                    channel.send(i) 
-                    startserver(sock)
-                    print "started server with socket"
-                    break
-            else:
-                channel.send(None) 
-    """)
-           
-    # open a gateway to a fresh child process 
+    import py 
+    port = 7770
+    socketserverbootstrap = py.code.Source(
+        mypath.dirpath().dirpath('bin', 'socketserver.py').read(),
+        """
+        import socket
+        sock = bind_and_listen(("localhost", %r)) 
+        channel.send("ok") 
+        startserver(sock)
+    """ % port) 
+    # open a gateway to a fresh child process
     proxygw = py.execnet.PopenGateway()
 
     # execute asynchronously the above socketserverbootstrap on the other
-    channel = proxygw.remote_exec_async(socketserverbootstrap) 
+    channel = proxygw.remote_exec(socketserverbootstrap)
 
-    # send socket-port range to the receiving for-loop
-    channel.send((7770, 7800))
-    
-    #
-    # the other side should start the for loop now, we
-    # wait for the result
-    #
-    listenport = channel.receive() 
-    if listenport is None: 
-        raise IOError, "could not setup remote SocketServer"
-   
-    # open another gateway to the freshly installed socket server 
-    socketgw = py.execnet.SocketGateway('localhost', listenport) 
-    print "initialized socket gateway on port", listenport 
-
-    # later you can exit / close down the gateways 
-    socketgw.exit()
-    proxygw.exit()
+    # the other side should start the socket server now 
+    assert channel.receive() == "ok" 
+    gw = py.execnet.SocketGateway('localhost', cls.port)
+    print "initialized socket gateway to port", cls.port
 
 .. _`py API`: api.html 
 

Deleted: /py/dist/py/execnet/bin/startserver.py
==============================================================================
--- /py/dist/py/execnet/bin/startserver.py	Sun Jan 16 13:33:03 2005
+++ (empty file)
@@ -1,87 +0,0 @@
-#! /usr/bin/env python
-
-"""
-    start socket based minimal readline exec server
-"""
-# this part of the program only executes on the server side
-#
-
-progname = 'socket_readline_exec_server-1.2'
-debug = 0
-
-import sys, socket, os
-try:
-    import fcntl
-except ImportError:
-    fcntl = None
-
-if debug: #  and not os.isatty(sys.stdin.fileno()):
-    f = open('/tmp/execnet-socket-pyout.log', 'a', 0)
-    old = sys.stdout, sys.stderr
-    sys.stdout = sys.stderr = f
-    #import py 
-    #compile = py.code.compile 
-
-def exec_from_one_connection(serversock):
-    print progname, 'Entering Accept loop', serversock.getsockname()
-    clientsock,address = serversock.accept()
-    print progname, 'got new connection from %s %s' % address
-    clientfile = clientsock.makefile('r+b',0)
-    print "reading line"
-    # rstrip so that we can use \r\n for telnet testing
-    source = clientfile.readline().rstrip()
-    clientfile.close()
-    g = {'clientsock' : clientsock, 'address' : address}
-    source = eval(source)
-    if source:
-        co = compile(source+'\n', source, 'exec')
-        print progname, 'compiled source, executing'
-        try:
-            exec co in g
-        finally:
-            print progname, 'finished executing code'
-            # background thread might hold a reference to this (!?)
-            #clientsock.close()
-
-def bind_and_listen(hostport):
-    if isinstance(hostport, str):
-        host, port = hostport.split(':')
-        hostport = (host, int(port))
-    serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-    # set close-on-exec
-    if hasattr(fcntl, 'FD_CLOEXEC'):
-        old = fcntl.fcntl(serversock.fileno(), fcntl.F_GETFD)
-        fcntl.fcntl(serversock.fileno(), fcntl.F_SETFD, old | fcntl.FD_CLOEXEC)
-    # allow the address to be re-used in a reasonable amount of time
-    if os.name == 'posix' and sys.platform != 'cygwin':
-        serversock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
-        
-    serversock.bind(hostport)
-    serversock.listen(5)
-    return serversock
-
-def startserver(serversock, loop=False):
-    try:
-        while 1: 
-            try: 
-                exec_from_one_connection(serversock)
-            except (KeyboardInterrupt, SystemExit): 
-                raise 
-            except: 
-                import traceback
-                traceback.print_exc()
-            if not loop: 
-                break 
-    finally:
-        print "leaving socketserver execloop"
-        serversock.shutdown(2)
-
-if __name__ == '__main__':
-    import sys
-    if len(sys.argv)>1:
-        hostport = sys.argv[1]
-    else:
-        hostport = ':8888'
-    serversock = bind_and_listen(hostport)
-    startserver(serversock, loop=True)
-

Modified: py/dist/py/execnet/testing/test_gateway.py
==============================================================================
--- py/dist/py/execnet/testing/test_gateway.py	(original)
+++ py/dist/py/execnet/testing/test_gateway.py	Sun Jan 16 13:33:03 2005
@@ -146,10 +146,9 @@
 
 class SocketGatewaySetup:
     def setup_class(cls):
-        cls.proxygw = py.execnet.PopenGateway()
         cls.port = 7770
         socketserverbootstrap = py.code.Source(
-            mypath.dirpath().dirpath('bin', 'startserver.py').read(),
+            mypath.dirpath().dirpath('bin', 'socketserver.py').read(),
             """
             import socket
             sock = bind_and_listen(("localhost", %r)) 



More information about the pytest-commit mailing list