summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCollin Winter <collinw@gmail.com>2007-09-10 00:47:20 (GMT)
committerCollin Winter <collinw@gmail.com>2007-09-10 00:47:20 (GMT)
commit4c6a140b7f6478d92e14d84c6ca89edef120658e (patch)
treeefc2e3d8b5ee29e47bcb48f2eae351b06d71d018
parent58721bca113398b58e774515360b8d04bf67e9ca (diff)
downloadcpython-4c6a140b7f6478d92e14d84c6ca89edef120658e.zip
cpython-4c6a140b7f6478d92e14d84c6ca89edef120658e.tar.gz
cpython-4c6a140b7f6478d92e14d84c6ca89edef120658e.tar.bz2
General clean-up in socket howto.
-rw-r--r--Doc/howto/sockets.rst49
1 files changed, 23 insertions, 26 deletions
diff --git a/Doc/howto/sockets.rst b/Doc/howto/sockets.rst
index dc05d32..fa72882 100644
--- a/Doc/howto/sockets.rst
+++ b/Doc/howto/sockets.rst
@@ -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::
#create an INET, STREAMing socket
- s = socket.socket(
- socket.AF_INET, socket.SOCK_STREAM)
+ 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))
@@ -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
mainloop of the web server::
- while 1:
+ while True:
#accept connections from outside
(clientsocket, address) = serversocket.accept()
#now do something with the clientsocket
@@ -185,38 +184,36 @@ Assuming you don't want to end the connection, the simplest solution is a fixed
length message::
class mysocket:
- '''demonstration class only
+ """demonstration class only
- coded for clarity, not efficiency
- '''
+ """
def __init__(self, sock=None):
- if sock is None:
- self.sock = socket.socket(
- socket.AF_INET, socket.SOCK_STREAM)
- else:
- self.sock = sock
+ if sock is None:
+ self.sock = socket.socket(
+ socket.AF_INET, socket.SOCK_STREAM)
+ else:
+ self.sock = sock
def connect(self, host, port):
- self.sock.connect((host, port))
+ self.sock.connect((host, port))
def mysend(self, msg):
- totalsent = 0
- while totalsent < MSGLEN:
- sent = self.sock.send(msg[totalsent:])
- if sent == 0:
- raise RuntimeError, \
- "socket connection broken"
- totalsent = totalsent + sent
+ totalsent = 0
+ while totalsent < MSGLEN:
+ sent = self.sock.send(msg[totalsent:])
+ if sent == 0:
+ raise RuntimeError("socket connection broken")
+ totalsent = totalsent + sent
def myreceive(self):
- msg = ''
- while len(msg) < MSGLEN:
- chunk = self.sock.recv(MSGLEN-len(msg))
- if chunk == '':
- raise RuntimeError, \
- "socket connection broken"
- msg = msg + chunk
- return msg
+ msg = ''
+ while len(msg) < MSGLEN:
+ chunk = self.sock.recv(MSGLEN-len(msg))
+ if chunk == '':
+ raise RuntimeError("socket connection broken")
+ msg = msg + chunk
+ return msg
The sending code here is usable for almost any messaging scheme - in Python you
send strings, and you can use ``len()`` to determine its length (even if it has