@@ -61,8 +61,7 @@ Roughly speaking, when you clicked on the link that brought you to this page,
6161your browser did something like the following::
6262
6363 #create an INET, STREAMing socket
64- s = socket.socket(
65- socket.AF_INET, socket.SOCK_STREAM)
64+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
6665 #now connect to the web server on port 80
6766 # - the normal http port
6867 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.
10099OK, now we have a "server" socket, listening on port 80. Now we enter the
101100mainloop of the web server::
102101
103- while 1 :
102+ while True :
104103 #accept connections from outside
105104 (clientsocket, address) = serversocket.accept()
106105 #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
185184length message::
186185
187186 class mysocket:
188- ''' demonstration class only
187+ """ demonstration class only
189188 - coded for clarity, not efficiency
190- '''
189+ """
191190
192191 def __init__(self, sock=None):
193- if sock is None:
194- self.sock = socket.socket(
195- socket.AF_INET, socket.SOCK_STREAM)
196- else:
197- self.sock = sock
192+ if sock is None:
193+ self.sock = socket.socket(
194+ socket.AF_INET, socket.SOCK_STREAM)
195+ else:
196+ self.sock = sock
198197
199198 def connect(self, host, port):
200- self.sock.connect((host, port))
199+ self.sock.connect((host, port))
201200
202201 def mysend(self, msg):
203- totalsent = 0
204- while totalsent < MSGLEN:
205- sent = self.sock.send(msg[totalsent:])
206- if sent == 0:
207- raise RuntimeError, \
208- "socket connection broken"
209- totalsent = totalsent + sent
202+ totalsent = 0
203+ while totalsent < MSGLEN:
204+ sent = self.sock.send(msg[totalsent:])
205+ if sent == 0:
206+ raise RuntimeError("socket connection broken")
207+ totalsent = totalsent + sent
210208
211209 def myreceive(self):
212- msg = ''
213- while len(msg) < MSGLEN:
214- chunk = self.sock.recv(MSGLEN-len(msg))
215- if chunk == '':
216- raise RuntimeError, \
217- "socket connection broken"
218- msg = msg + chunk
219- return msg
210+ msg = ''
211+ while len(msg) < MSGLEN:
212+ chunk = self.sock.recv(MSGLEN-len(msg))
213+ if chunk == '':
214+ raise RuntimeError("socket connection broken")
215+ msg = msg + chunk
216+ return msg
220217
221218The sending code here is usable for almost any messaging scheme - in Python you
222219send strings, and you can use ``len() `` to determine its length (even if it has
0 commit comments