Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Doc/library/socketserver.rst

    r2 r391  
    1 
    21:mod:`SocketServer` --- A framework for network servers
    32=======================================================
     
    98
    109   The :mod:`SocketServer` module has been renamed to :mod:`socketserver` in
    11    Python 3.0.  The :term:`2to3` tool will automatically adapt imports when
    12    converting your sources to 3.0.
    13 
     10   Python 3.  The :term:`2to3` tool will automatically adapt imports when
     11   converting your sources to Python 3.
     12
     13**Source code:** :source:`Lib/SocketServer.py`
     14
     15--------------
    1416
    1517The :mod:`SocketServer` module simplifies the task of writing network servers.
     
    8688
    8789The mix-in class must come first, since it overrides a method defined in
    88 :class:`UDPServer`.  Setting the various member variables also changes the
     90:class:`UDPServer`.  Setting the various attributes also change the
    8991behavior of the underlying server mechanism.
    9092
     
    115117
    116118Another approach to handling multiple simultaneous requests in an environment
    117 that supports neither threads nor :func:`fork` (or where these are too expensive
    118 or inappropriate for the service) is to maintain an explicit table of partially
    119 finished requests and to use :func:`select` to decide which request to work on
    120 next (or whether to handle a new incoming request).  This is particularly
    121 important for stream services where each client can potentially be connected for
    122 a long time (if threads or subprocesses cannot be used). See :mod:`asyncore` for
    123 another way to manage this.
     119that supports neither threads nor :func:`~os.fork` (or where these are too
     120expensive or inappropriate for the service) is to maintain an explicit table of
     121partially finished requests and to use :func:`~select.select` to decide which
     122request to work on next (or whether to handle a new incoming request).  This is
     123particularly important for stream services where each client can potentially be
     124connected for a long time (if threads or subprocesses cannot be used). See
     125:mod:`asyncore` for another way to manage this.
    124126
    125127.. XXX should data and methods be intermingled, or separate?
     
    157159.. method:: BaseServer.serve_forever(poll_interval=0.5)
    158160
    159    Handle requests until an explicit :meth:`shutdown` request.  Polls for
    160    shutdown every *poll_interval* seconds.
     161   Handle requests until an explicit :meth:`shutdown` request.
     162   Poll for shutdown every *poll_interval* seconds. Ignores :attr:`self.timeout`.
     163   If you need to do periodic tasks, do them in another thread.
    161164
    162165
    163166.. method:: BaseServer.shutdown()
    164167
    165    Tells the :meth:`serve_forever` loop to stop and waits until it does.
     168   Tell the :meth:`serve_forever` loop to stop and wait until it does.
    166169
    167170   .. versionadded:: 2.6
     
    304307
    305308   Called after the :meth:`handle` method to perform any clean-up actions
    306    required.  The default implementation does nothing.  If :meth:`setup` or
    307    :meth:`handle` raise an exception, this function will not be called.
     309   required.  The default implementation does nothing.  If :meth:`setup`
     310   raises an exception, this function will not be called.
    308311
    309312
     
    355358           # self.request is the TCP socket connected to the client
    356359           self.data = self.request.recv(1024).strip()
    357            print "%s wrote:" % self.client_address[0]
     360           print "{} wrote:".format(self.client_address[0])
    358361           print self.data
    359362           # just send back the same data, but upper-cased
    360            self.request.send(self.data.upper())
     363           self.request.sendall(self.data.upper())
    361364
    362365   if __name__ == "__main__":
     
    379382           # we can now use e.g. readline() instead of raw recv() calls
    380383           self.data = self.rfile.readline().strip()
    381            print "%s wrote:" % self.client_address[0]
     384           print "{} wrote:".format(self.client_address[0])
    382385           print self.data
    383386           # Likewise, self.wfile is a file-like object used to write back
     
    388391``recv()`` multiple times until it encounters a newline character, while the
    389392single ``recv()`` call in the first handler will just return what has been sent
    390 from the client in one ``send()`` call.
     393from the client in one ``sendall()`` call.
    391394
    392395
     
    402405   sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    403406
    404    # Connect to server and send data
    405    sock.connect((HOST, PORT))
    406    sock.send(data + "\n")
    407 
    408    # Receive data from the server and shut down
    409    received = sock.recv(1024)
    410    sock.close()
    411 
    412    print "Sent:     %s" % data
    413    print "Received: %s" % received
     407   try:
     408       # Connect to server and send data
     409       sock.connect((HOST, PORT))
     410       sock.sendall(data + "\n")
     411
     412       # Receive data from the server and shut down
     413       received = sock.recv(1024)
     414   finally:
     415       sock.close()
     416
     417   print "Sent:     {}".format(data)
     418   print "Received: {}".format(received)
    414419
    415420
     
    452457           data = self.request[0].strip()
    453458           socket = self.request[1]
    454            print "%s wrote:" % self.client_address[0]
     459           print "{} wrote:".format(self.client_address[0])
    455460           print data
    456461           socket.sendto(data.upper(), self.client_address)
    457462
    458463   if __name__ == "__main__":
    459       HOST, PORT = "localhost", 9999
    460       server = SocketServer.UDPServer((HOST, PORT), MyUDPHandler)
    461       server.serve_forever()
     464       HOST, PORT = "localhost", 9999
     465       server = SocketServer.UDPServer((HOST, PORT), MyUDPHandler)
     466       server.serve_forever()
    462467
    463468This is the client side::
     
    477482   received = sock.recv(1024)
    478483
    479    print "Sent:     %s" % data
    480    print "Received: %s" % received
     484   print "Sent:     {}".format(data)
     485   print "Received: {}".format(received)
    481486
    482487The output of the example should look exactly like for the TCP server example.
     
    499504       def handle(self):
    500505           data = self.request.recv(1024)
    501            cur_thread = threading.currentThread()
    502            response = "%s: %s" % (cur_thread.getName(), data)
    503            self.request.send(response)
     506           cur_thread = threading.current_thread()
     507           response = "{}: {}".format(cur_thread.name, data)
     508           self.request.sendall(response)
    504509
    505510   class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
     
    509514       sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    510515       sock.connect((ip, port))
    511        sock.send(message)
    512        response = sock.recv(1024)
    513        print "Received: %s" % response
    514        sock.close()
     516       try:
     517           sock.sendall(message)
     518           response = sock.recv(1024)
     519           print "Received: {}".format(response)
     520       finally:
     521           sock.close()
    515522
    516523   if __name__ == "__main__":
     
    525532       server_thread = threading.Thread(target=server.serve_forever)
    526533       # Exit the server thread when the main thread terminates
    527        server_thread.setDaemon(True)
     534       server_thread.daemon = True
    528535       server_thread.start()
    529        print "Server loop running in thread:", server_thread.getName()
     536       print "Server loop running in thread:", server_thread.name
    530537
    531538       client(ip, port, "Hello World 1")
     
    534541
    535542       server.shutdown()
     543
    536544
    537545The output of the example should look something like this::
Note: See TracChangeset for help on using the changeset viewer.