mirror of
https://github.com/python/cpython.git
synced 2025-11-03 03:22:27 +00:00
General clean-up in socket howto.
This commit is contained in:
parent
58721bca11
commit
4c6a140b7f
1 changed files with 23 additions and 26 deletions
|
|
@ -61,8 +61,7 @@ Roughly speaking, when you clicked on the link that brought you to this page,
|
||||||
your browser did something like the following::
|
your browser did something like the following::
|
||||||
|
|
||||||
#create an INET, STREAMing socket
|
#create an INET, STREAMing socket
|
||||||
s = socket.socket(
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
#now connect to the web server on port 80
|
#now connect to the web server on port 80
|
||||||
# - the normal http port
|
# - the normal http port
|
||||||
s.connect(("www.mcmillan-inc.com", 80))
|
s.connect(("www.mcmillan-inc.com", 80))
|
||||||
|
|
@ -100,7 +99,7 @@ connections. If the rest of the code is written properly, that should be plenty.
|
||||||
OK, now we have a "server" socket, listening on port 80. Now we enter the
|
OK, now we have a "server" socket, listening on port 80. Now we enter the
|
||||||
mainloop of the web server::
|
mainloop of the web server::
|
||||||
|
|
||||||
while 1:
|
while True:
|
||||||
#accept connections from outside
|
#accept connections from outside
|
||||||
(clientsocket, address) = serversocket.accept()
|
(clientsocket, address) = serversocket.accept()
|
||||||
#now do something with the clientsocket
|
#now do something with the clientsocket
|
||||||
|
|
@ -185,9 +184,9 @@ Assuming you don't want to end the connection, the simplest solution is a fixed
|
||||||
length message::
|
length message::
|
||||||
|
|
||||||
class mysocket:
|
class mysocket:
|
||||||
'''demonstration class only
|
"""demonstration class only
|
||||||
- coded for clarity, not efficiency
|
- coded for clarity, not efficiency
|
||||||
'''
|
"""
|
||||||
|
|
||||||
def __init__(self, sock=None):
|
def __init__(self, sock=None):
|
||||||
if sock is None:
|
if sock is None:
|
||||||
|
|
@ -204,8 +203,7 @@ length message::
|
||||||
while totalsent < MSGLEN:
|
while totalsent < MSGLEN:
|
||||||
sent = self.sock.send(msg[totalsent:])
|
sent = self.sock.send(msg[totalsent:])
|
||||||
if sent == 0:
|
if sent == 0:
|
||||||
raise RuntimeError, \
|
raise RuntimeError("socket connection broken")
|
||||||
"socket connection broken"
|
|
||||||
totalsent = totalsent + sent
|
totalsent = totalsent + sent
|
||||||
|
|
||||||
def myreceive(self):
|
def myreceive(self):
|
||||||
|
|
@ -213,8 +211,7 @@ length message::
|
||||||
while len(msg) < MSGLEN:
|
while len(msg) < MSGLEN:
|
||||||
chunk = self.sock.recv(MSGLEN-len(msg))
|
chunk = self.sock.recv(MSGLEN-len(msg))
|
||||||
if chunk == '':
|
if chunk == '':
|
||||||
raise RuntimeError, \
|
raise RuntimeError("socket connection broken")
|
||||||
"socket connection broken"
|
|
||||||
msg = msg + chunk
|
msg = msg + chunk
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue