[Python-checkins] cpython (merge 3.2 -> default): Merge assorted fixes from 3.2
antoine.pitrou
python-checkins at python.org
Mon Dec 5 01:53:08 CET 2011
http://hg.python.org/cpython/rev/f2be1e010cc8
changeset: 73864:f2be1e010cc8
parent: 73859:8701f6373d0b
parent: 73863:c34188efb965
user: Antoine Pitrou <solipsis at pitrou.net>
date: Mon Dec 05 01:47:40 2011 +0100
summary:
Merge assorted fixes from 3.2
files:
Doc/howto/sockets.rst | 36 ++++++++++++++----------------
1 files changed, 17 insertions(+), 19 deletions(-)
diff --git a/Doc/howto/sockets.rst b/Doc/howto/sockets.rst
--- a/Doc/howto/sockets.rst
+++ b/Doc/howto/sockets.rst
@@ -60,11 +60,10 @@
Roughly speaking, when you clicked on the link that brought you to this page,
your browser did something like the following::
- #create an INET, STREAMing socket
+ # create an INET, STREAMing socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- #now connect to the web server on port 80
- # - the normal http port
- s.connect(("www.mcmillan-inc.com", 80))
+ # now connect to the web server on port 80 - the normal http port
+ s.connect(("www.python.org", 80))
When the ``connect`` completes, the socket ``s`` can be used to send
in a request for the text of the page. The same socket will read the
@@ -75,13 +74,11 @@
What happens in the web server is a bit more complex. First, the web server
creates a "server socket"::
- #create an INET, STREAMing socket
- serversocket = socket.socket(
- socket.AF_INET, socket.SOCK_STREAM)
- #bind the socket to a public host,
- # and a well-known port
+ # create an INET, STREAMing socket
+ serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ # bind the socket to a public host, and a well-known port
serversocket.bind((socket.gethostname(), 80))
- #become a server socket
+ # become a server socket
serversocket.listen(5)
A couple things to notice: we used ``socket.gethostname()`` so that the socket
@@ -101,10 +98,10 @@
mainloop of the web server::
while True:
- #accept connections from outside
+ # accept connections from outside
(clientsocket, address) = serversocket.accept()
- #now do something with the clientsocket
- #in this case, we'll pretend this is a threaded server
+ # now do something with the clientsocket
+ # in this case, we'll pretend this is a threaded server
ct = client_thread(clientsocket)
ct.run()
@@ -126,12 +123,13 @@
---
If you need fast IPC between two processes on one machine, you should look into
-whatever form of shared memory the platform offers. A simple protocol based
-around shared memory and locks or semaphores is by far the fastest technique.
+pipes or shared memory. If you do decide to use AF_INET sockets, bind the
+"server" socket to ``'localhost'``. On most platforms, this will take a
+shortcut around a couple of layers of network code and be quite a bit faster.
-If you do decide to use sockets, bind the "server" socket to ``'localhost'``. On
-most platforms, this will take a shortcut around a couple of layers of network
-code and be quite a bit faster.
+.. seealso::
+ The :mod:`multiprocessing` integrates cross-platform IPC into a higher-level
+ API.
Using a Socket
@@ -300,7 +298,7 @@
Probably the worst thing about using blocking sockets is what happens when the
other side comes down hard (without doing a ``close``). Your socket is likely to
-hang. SOCKSTREAM is a reliable protocol, and it will wait a long, long time
+hang. TCP is a reliable protocol, and it will wait a long, long time
before giving up on a connection. If you're using threads, the entire thread is
essentially dead. There's not much you can do about it. As long as you aren't
doing something dumb, like holding a lock while doing a blocking read, the
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list