1 "Socket wrapper for Windows, which does not support dup()."
3 # (And hence, fromfd() and makefile() are unimplemented in C....)
5 # XXX Living dangerously here -- close() is implemented by deleting a
6 # reference. Thus we rely on the real _socket module to close on
7 # deallocation, and also hope that nobody keeps a reference to our _sock
17 _realsocketcall
= socket
20 def socket(family
, type, proto
=0):
21 return _socketobject(_realsocketcall(family
, type, proto
))
26 def __init__(self
, sock
):
36 sock
, addr
= self
._sock
.accept()
37 return _socketobject(sock
), addr
40 return _socketobject(self
._sock
)
42 def makefile(self
, mode
='r', bufsize
=-1):
43 return _fileobject(self
._sock
, mode
, bufsize
)
45 _s
= "def %s(self, *args): return apply(self._sock.%s, args)\n\n"
46 for _m
in ('bind', 'connect', 'connect_ex', 'fileno', 'listen',
47 'getpeername', 'getsockname',
48 'getsockopt', 'setsockopt',
49 'recv', 'recvfrom', 'send', 'sendto',
57 def __init__(self
, sock
, mode
, bufsize
):
62 self
._rbufsize
= max(1, bufsize
)
63 self
._wbufsize
= bufsize
64 self
._wbuf
= self
._rbuf
= ""
78 self
._sock
.send(self
._wbuf
)
82 return self
._sock
.fileno()
84 def write(self
, data
):
85 self
._wbuf
= self
._wbuf
+ data
86 if self
._wbufsize
== 1:
90 if len(self
._wbuf
) >= self
._wbufsize
:
93 def writelines(self
, list):
94 filter(self
._sock
.send
, list)
101 data
= self
._rbuf
[:n
]
102 self
._rbuf
= self
._rbuf
[n
:]
108 new
= self
._sock
.recv(max(n
, self
._rbufsize
))
118 k
= max(512, self
._rbufsize
)
122 new
= self
._sock
.recv(k
)
125 k
= min(k
*2, 1024**2)
128 def readline(self
, limit
=-1):
130 i
= self
._rbuf
.find('\n')
131 while i
< 0 and not (0 < limit
<= len(self
._rbuf
)):
132 new
= self
._sock
.recv(self
._rbufsize
)
135 if i
>= 0: i
= i
+ len(self
._rbuf
)
136 self
._rbuf
= self
._rbuf
+ new
137 if i
< 0: i
= len(self
._rbuf
)
139 if 0 <= limit
< len(self
._rbuf
): i
= limit
140 data
, self
._rbuf
= self
._rbuf
[:i
], self
._rbuf
[i
:]
146 line
= self
.readline()
154 errorTab
[10004] = "The operation was interrupted."
155 errorTab
[10009] = "A bad file handle was passed."
156 errorTab
[10013] = "Permission denied."
157 errorTab
[10014] = "A fault occurred on the network??" # WSAEFAULT
158 errorTab
[10022] = "An invalid operation was attempted."
159 errorTab
[10035] = "The socket operation would block"
160 errorTab
[10036] = "A blocking operation is already in progress."
161 errorTab
[10048] = "The network address is in use."
162 errorTab
[10054] = "The connection has been reset."
163 errorTab
[10058] = "The network has been shut down."
164 errorTab
[10060] = "The operation timed out."
165 errorTab
[10061] = "Connection refused."
166 errorTab
[10063] = "The name is too long."
167 errorTab
[10064] = "The host is down."
168 errorTab
[10065] = "The host is unreachable."