/
inetcomms.py
311 lines (248 loc) · 7.82 KB
/
inetcomms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python
#
# InetComms.py
# Copyright (C) 2003 Mike Leonhard
# http://tamale.net/
#
# version 1.3 (2003/07/26 23:52)
# * ListenClass is for IPv4
# * ListenClass6 is for IPv6
# version 1.2 (2003/07/26 23:28)
# * moved imports into individual classes
# this allows server to be run on old Python 1.5 installations that lack threads
# version 1.1 (2003/07/07 02:07)
# * changed RepresentativeClass to HeadHunterClass
# HeadHunterClass does multi-threaded connect in separate thread
# callback is used to notify main program of success/failure
# supports IPv6
# * ListenClass also support IPv6 now, too
# version 1 RC2 (2003/07/06 02:05)
# * sockets are now non-blocking
# outgoing data is queued up
# Coordinator must select on liasons with pending unwritten data and call liason.dosend()
# version 1 RC1 (2003/07/06 01:01)
# * initial version
# import required modules
import socket
# try to import optional modules
try: import select, thread
except ImportError: pass
CHUNKSIZE = 512 # send chunk size
class LiasonClass:
def __init__( self, s, prettyaddress=None, greeting=None ):
"""Initialize the Liason class, greet, and announce"""
self.socket = s
self.prettyaddress = prettyaddress
self.connected = 1
self.outbuffer = ""
# load the required modules
import select
# human readable address not provided
if not self.prettyaddress:
# lookup our peer's name and use that
(phost, pport) = s.getpeername()
self.prettyaddress = str( phost ) + ":" + str( pport )
# set socket to non-blocking mode
self.socket.setblocking( 0 )
# announce the new connection
print 'connected with ' + self.prettyaddress
# greeting
if greeting: self.dosend( greeting )
def fileno( self ):
"""Return the file number of the socket"""
return self.socket.fileno()
def doread( self ):
"""Read data from the socket"""
# not connected
if not self.connected: return None
# catch exceptions
try:
# Read
data = self.socket.recv( 1024 )
# treat empty read as error
if len( data ) == 0: raise socket.error
# error reading
except socket.error:
# disconnect the socket
self.disconnect()
# no data to return
return None
# return the data that was read
return data
def dosend( self, data=None ):
"""Send data on the socket"""
# not connected
if not self.connected: return
# data was specified
if data:
# add outgoing data to buffer
self.outbuffer = self.outbuffer + data
# data is remaining to be sent
while len( self.outbuffer ):
# catch exceptions
try:
# send first chunk
sent = self.socket.send( self.outbuffer[:CHUNKSIZE] )
# cut out sent data from buffer
self.outbuffer = self.outbuffer[sent:]
# socket is not ready
except socket.error:
# TODO: Disconnect slow clients
print self.prettyaddress + 'send queue length is ' + str( len( self.outbuffer ) )
# try again later
break
def disconnect( self ):
"""Disconnect the socket, announce"""
# not connected
if not self.connected: return None
# TODO: read all available data before socket close (polite way)
# announce the disconnection
print 'disconnected with ' + self.prettyaddress
# close the socket
self.socket.close()
self.connected = 0
def selectandread( self, wait ):
# not connected
if not self.connected: return None
# poll socket
(readable, w, e) = select.select( [self.socket.fileno()], [], [], wait )
# no data
if len( readable ) == 0: return None
# read the data
return self.doread()
class HeadHunterClass:
"""Sockets communication class that does non-blocking connect()"""
def __init__( self, host, port, prettyaddress=None, callback=None ):
"""Initialize the Representative class, spawn the connector thread"""
self.host = host
self.port = port
self.prettyaddress = prettyaddress
self.callback = callback
self.l = None
self.errotext = None
# load required modules
import thread
# no callback specified
if self.callback == None:
# use donothing callback
self.callback = self.donothing
# spawn the new thread
thread.start_new_thread( self.connectorthread, () )
def donothing( self, headhunter ):
"""Generic callback function"""
pass
def connectorthread( self ):
# resolve the address
try:
addrsetlist = socket.getaddrinfo( self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM)
# lookup error
except socket.gaierror, (error, text):
self.errortext = "GAI ERROR: " + text
return self.callback( self )
# we want to create the proper socket to connect to the server
s = None
# try each address
for addrset in addrsetlist:
# break out the pieces of the address
af, socktype, proto, canonname, sa = addrset
print 'addrset: ' + str( addrset )
# create the socket
try: s = socket.socket( af, socktype, proto )
# unsupported socket configuration
except socket.error, (error, text):
# keep the error message
self.errortext = "SOCKET ERROR: " + text
# try next address
continue
# connect to the server
try: s.connect( sa )
# connect failure
except socket.error, (error, text):
# close and discard the socket
s.close()
s = None
# keep the error message
self.errortext = "CONNECT ERROR: " + text
# try the next address
continue
# connect was successful
break
# none of the addresses were successful
if s is None:
# report the error
return self.callback( self )
# create the liason class
self.l = LiasonClass( s, self.prettyaddress )
# done
self.callback( self )
class ListenClass:
def __init__( self, port, host=None, addressfamily=socket.AF_INET ):
"""Initialize Listen class"""
# data
self.newconnections = []
# resolve our own address
print 'resolving ' + str( host ) + ':' + str( port )
addrsetlist = socket.getaddrinfo( host, port, addressfamily, socket.SOCK_STREAM, 0, socket.AI_PASSIVE )
# we want to create the proper socket to listen on the address and port
s = None
# try each address
for addrset in addrsetlist:
# break out the pieces of the address
af, socktype, proto, canonname, sa = addrset
print 'addrset: ' + str( addrset )
# catch exceptions
try:
# create the socket
s = socket.socket( af, socktype, proto )
# set option to allow quick restart of server
s.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
# unsupported socket configuration
except socket.error, exception:
# keep the error
self.exception = exception
# try next address
continue
# catch exceptions
try:
# bind to the address
s.bind( sa )
# make the socket able to accept connections (max 5 connections can wait at a time)
s.listen( 5 )
# bind failure
except socket.error, exception:
# close and discard the socket
s.close()
s = None
# keep the error
self.exception = exception
# try next address
continue
# bind was successful
break
# none of the addresses were successful
if s is None:
# raise the exception again
raise socket.error, self.exception
# listening on socket
print 'Listening on', s.getsockname()
# socket is ready
self.socket = s
def fileno( self ):
"""Return the file number of the socket"""
return self.socket.fileno()
def doread( self ):
"""Accept a new connection on the listening socket"""
# accept the connection
conn, addr = self.socket.accept()
# add it to the list
self.newconnections.append( (conn, addr) )
def disconnect( self ):
# make the socket unable to accept new connections
self.socket.listen( 0 )
# close the socket
self.socket.close()
print 'Stopped listening'
class ListenClass6( ListenClass ):
"""IPv6 ListenClass"""
def __init__( self, port, host=None ):
ListenClass.__init__( self, port, host, socket.AF_INET6 )