@@ -60,11 +60,10 @@ Creating a Socket
6060Roughly speaking, when you clicked on the link that brought you to this page,
6161your browser did something like the following::
6262
63- #create an INET, STREAMing socket
63+ # create an INET, STREAMing socket
6464 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
65- #now connect to the web server on port 80
66- # - the normal http port
67- s.connect(("www.mcmillan-inc.com", 80))
65+ # now connect to the web server on port 80 - the normal http port
66+ s.connect(("www.python.org", 80))
6867
6968When the ``connect `` completes, the socket ``s `` can be used to send
7069in a request for the text of the page. The same socket will read the
@@ -75,13 +74,11 @@ exchanges).
7574What happens in the web server is a bit more complex. First, the web server
7675creates a "server socket"::
7776
78- #create an INET, STREAMing socket
79- serversocket = socket.socket(
80- socket.AF_INET, socket.SOCK_STREAM)
81- #bind the socket to a public host,
82- # and a well-known port
77+ # create an INET, STREAMing socket
78+ serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
79+ # bind the socket to a public host, and a well-known port
8380 serversocket.bind((socket.gethostname(), 80))
84- #become a server socket
81+ # become a server socket
8582 serversocket.listen(5)
8683
8784A couple things to notice: we used ``socket.gethostname() `` so that the socket
@@ -101,10 +98,10 @@ Now that we have a "server" socket, listening on port 80, we can enter the
10198mainloop of the web server::
10299
103100 while True:
104- #accept connections from outside
101+ # accept connections from outside
105102 (clientsocket, address) = serversocket.accept()
106- #now do something with the clientsocket
107- #in this case, we'll pretend this is a threaded server
103+ # now do something with the clientsocket
104+ # in this case, we'll pretend this is a threaded server
108105 ct = client_thread(clientsocket)
109106 ct.run()
110107
@@ -126,12 +123,13 @@ IPC
126123---
127124
128125If you need fast IPC between two processes on one machine, you should look into
129- whatever form of shared memory the platform offers. A simple protocol based
130- around shared memory and locks or semaphores is by far the fastest technique.
126+ pipes or shared memory. If you do decide to use AF_INET sockets, bind the
127+ "server" socket to ``'localhost' ``. On most platforms, this will take a
128+ shortcut around a couple of layers of network code and be quite a bit faster.
131129
132- If you do decide to use sockets, bind the "server" socket to `` 'localhost' ``. On
133- most platforms, this will take a shortcut around a couple of layers of network
134- code and be quite a bit faster .
130+ .. seealso ::
131+ The :mod: ` multiprocessing ` integrates cross-platform IPC into a higher-level
132+ API .
135133
136134
137135Using a Socket
@@ -300,7 +298,7 @@ When Sockets Die
300298
301299Probably the worst thing about using blocking sockets is what happens when the
302300other side comes down hard (without doing a ``close ``). Your socket is likely to
303- hang. SOCKSTREAM is a reliable protocol, and it will wait a long, long time
301+ hang. TCP is a reliable protocol, and it will wait a long, long time
304302before giving up on a connection. If you're using threads, the entire thread is
305303essentially dead. There's not much you can do about it. As long as you aren't
306304doing something dumb, like holding a lock while doing a blocking read, the
0 commit comments