Lines Matching full:socket
4 Socket Programming HOWTO
25 better behavior and performance from a STREAM socket than anything else. I will
26 try to clear up the mystery of what a socket is, as well as some hints on how to
31 Part of the trouble with understanding these things is that "socket" can mean a
33 distinction between a "client" socket - an endpoint of a conversation, and a
34 "server" socket, which is more like a switchboard operator. The client
53 Creating a Socket
59 # create an INET, STREAMing socket
60 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
64 When the ``connect`` completes, the socket ``s`` can be used to send
65 in a request for the text of the page. The same socket will read the
71 creates a "server socket"::
73 # create an INET, STREAMing socket
74 serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
75 # bind the socket to a public host, and a well-known port
76 serversocket.bind((socket.gethostname(), 80))
77 # become a server socket
80 A couple things to notice: we used ``socket.gethostname()`` so that the socket
82 80))`` or ``s.bind(('127.0.0.1', 80))`` we would still have a "server" socket,
84 specifies that the socket is reachable by any address the machine happens to
91 Finally, the argument to ``listen`` tells the socket library that we want it to
95 Now that we have a "server" socket, listening on port 80, we can enter the
109 multiplex between our "server" socket and any active ``clientsocket``\ s using
111 this: this is *all* a "server" socket does. It doesn't send any data. It doesn't
113 created in response to some *other* "client" socket doing a ``connect()`` to the
125 "server" socket to ``'localhost'``. On most platforms, this will take a
133 Using a Socket
136 The first thing to note, is that the web browser's "client" socket and the web
137 server's "client" socket are identical beasts. That is, this is a "peer to peer"
140 ``connect``\ ing socket starts the conversation, by sending in a request, or
144 and ``recv``, or you can transform your client socket into a file-like beast and
165 A protocol like HTTP uses a socket for only one transfer. The client sends a
166 request, then reads a reply. That's it. The socket is discarded. This means that
169 But if you plan to reuse your socket for further transfers, you need to realize
170 that *there is no* :abbr:`EOT (End of Transfer)` *on a socket.* I repeat: if a socket
173 forever, because the socket will *not* tell you that there's nothing more to
190 self.sock = socket.socket(
191 socket.AF_INET, socket.SOCK_STREAM)
203 raise RuntimeError("socket connection broken")
212 raise RuntimeError("socket connection broken")
254 It is perfectly possible to send binary data over a socket. The major problem is
262 Socket libraries have calls for converting 16 and 32 bit integers - ``ntohl,
279 Strictly speaking, you're supposed to use ``shutdown`` on a socket before you
280 ``close`` it. The ``shutdown`` is an advisory to the socket at the other end.
283 socket libraries, however, are so used to programmers neglecting to use this
294 Python takes the automatic shutdown a step further, and says that when a socket
296 relying on this is a very bad habit. If your socket just disappears without
297 doing a ``close``, the socket at the other end may hang indefinitely, thinking
305 other side comes down hard (without doing a ``close``). Your socket is likely to
325 In Python, you use ``socket.setblocking(False)`` to make it non-blocking. In C, it's
329 do this after creating the socket, but before using it. (Actually, if you're
355 You should note that a socket can go into more than one list. The ``select``
364 If a socket is in the output readable list, you can be
366 socket will return *something*. Same idea for the writable list. You'll be able
368 nothing. (Actually, any reasonably healthy socket will return as writable - it
371 If you have a "server" socket, put it in the potential_readers list. If it comes
373 have created a new socket to ``connect`` to someone else, put it in the
378 determining whether you will block - the socket returns as readable when there's
384 only. Also note that in C, many of the more advanced socket options are done