1 # Wrapper module for _socket, providing some additional facilities
2 # implemented in Python.
5 This module provides socket operations and some related functions.
6 On Unix, it supports IP (Internet Protocol) and Unix domain sockets.
7 On other systems, it only supports IP. Functions specific for a
8 socket are available as methods of the socket object.
12 socket() -- create a new socket object
13 fromfd() -- create a socket object from an open file descriptor [*]
14 gethostname() -- return the current hostname
15 gethostbyname() -- map a hostname to its IP number
16 gethostbyaddr() -- map an IP number or hostname to DNS info
17 getservbyname() -- map a service name and a protocol name to a port number
18 getprotobyname() -- mape a protocol name (e.g. 'tcp') to a number
19 ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
20 htons(), htonl() -- convert 16, 32 bit int from host to network byte order
21 inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format
22 inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)
23 ssl() -- secure socket layer support (only available if configured)
24 socket.getdefaulttimeout() -- get the default timeout value
25 socket.setdefaulttimeout() -- set the default timeout value
27 [*] not available on all platforms!
31 SocketType -- type object for socket objects
32 error -- exception raised for I/O errors
36 AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
37 SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
39 Many other constants may be defined; these may be used in calls to
40 the setsockopt() and getsockopt() methods.
57 __all__
.extend(os
._get
_exports
_list
(_socket
))
59 __all__
.extend(os
._get
_exports
_list
(_ssl
))
63 if (sys
.platform
.lower().startswith("win")
64 or (hasattr(os
, 'uname') and os
.uname()[0] == "BeOS")
65 or sys
.platform
=="riscos"):
71 def ssl(sock
, keyfile
=None, certfile
=None):
72 if hasattr(sock
, "_sock"):
74 return _realssl(sock
, keyfile
, certfile
)
77 if sys
.platform
.lower().startswith("win"):
79 errorTab
[10004] = "The operation was interrupted."
80 errorTab
[10009] = "A bad file handle was passed."
81 errorTab
[10013] = "Permission denied."
82 errorTab
[10014] = "A fault occurred on the network??" # WSAEFAULT
83 errorTab
[10022] = "An invalid operation was attempted."
84 errorTab
[10035] = "The socket operation would block"
85 errorTab
[10036] = "A blocking operation is already in progress."
86 errorTab
[10048] = "The network address is in use."
87 errorTab
[10054] = "The connection has been reset."
88 errorTab
[10058] = "The network has been shut down."
89 errorTab
[10060] = "The operation timed out."
90 errorTab
[10061] = "Connection refused."
91 errorTab
[10063] = "The name is too long."
92 errorTab
[10064] = "The host is down."
93 errorTab
[10065] = "The host is unreachable."
94 __all__
.append("errorTab")
100 """Get fully qualified domain name from name.
102 An empty argument is interpreted as meaning the local host.
104 First the hostname returned by gethostbyaddr() is checked, then
105 possibly existing aliases. In case no FQDN is available, hostname
109 if not name
or name
== '0.0.0.0':
112 hostname
, aliases
, ipaddrs
= gethostbyaddr(name
)
116 aliases
.insert(0, hostname
)
126 # These classes are used by the socket() defined on Windows and BeOS
127 # platforms to provide a best-effort implementation of the cleanup
128 # semantics needed when sockets can't be dup()ed.
130 # These are not actually used on other platforms.
134 'bind', 'connect', 'connect_ex', 'fileno', 'listen',
135 'getpeername', 'getsockname', 'getsockopt', 'setsockopt',
136 'recv', 'recvfrom', 'send', 'sendall', 'sendto', 'setblocking',
137 'settimeout', 'gettimeout', 'shutdown')
139 class _closedsocket(object):
141 def __getattr__(self
, name
):
142 raise error(9, 'Bad file descriptor')
144 class _socketobject(object):
146 __doc__
= _realsocket
.__doc
__
148 __slots__
= ["_sock"]
150 def __init__(self
, family
=AF_INET
, type=SOCK_STREAM
, proto
=0, _sock
=None):
152 _sock
= _realsocket(family
, type, proto
)
156 self
._sock
= _closedsocket()
157 close
.__doc
__ = _realsocket
.close
.__doc
__
160 sock
, addr
= self
._sock
.accept()
161 return _socketobject(_sock
=sock
), addr
162 accept
.__doc
__ = _realsocket
.accept
.__doc
__
165 """dup() -> socket object
167 Return a new socket object connected to the same system resource."""
168 return _socketobject(_sock
=self
._sock
)
170 def makefile(self
, mode
='r', bufsize
=-1):
171 """makefile([mode[, bufsize]]) -> file object
173 Return a regular file object corresponding to the socket. The mode
174 and bufsize arguments are as for the built-in open() function."""
175 return _fileobject(self
._sock
, mode
, bufsize
)
177 _s
= ("def %s(self, *args): return self._sock.%s(*args)\n\n"
178 "%s.__doc__ = _realsocket.%s.__doc__\n")
179 for _m
in _socketmethods
:
180 exec _s
% (_m
, _m
, _m
, _m
)
184 socket
= SocketType
= _socketobject
186 class _fileobject(object):
187 """Faux file object attached to a socket object."""
189 default_bufsize
= 8192
192 __slots__
= ["mode", "bufsize", "softspace",
193 # "closed" is a property, see below
194 "_sock", "_rbufsize", "_wbufsize", "_rbuf", "_wbuf"]
196 def __init__(self
, sock
, mode
='rb', bufsize
=-1):
198 self
.mode
= mode
# Not actually used in this version
200 bufsize
= self
.default_bufsize
201 self
.bufsize
= bufsize
202 self
.softspace
= False
206 self
._rbufsize
= self
.default_bufsize
208 self
._rbufsize
= bufsize
209 self
._wbufsize
= bufsize
210 self
._rbuf
= "" # A string
211 self
._wbuf
= [] # A list of strings
213 def _getclosed(self
):
214 return self
._sock
is not None
215 closed
= property(_getclosed
, doc
="True if the file is closed")
229 buffer = "".join(self
._wbuf
)
231 self
._sock
.sendall(buffer)
234 return self
._sock
.fileno()
236 def write(self
, data
):
237 data
= str(data
) # XXX Should really reject non-string non-buffers
240 self
._wbuf
.append(data
)
241 if (self
._wbufsize
== 0 or
242 self
._wbufsize
== 1 and '\n' in data
or
243 self
._get
_wbuf
_len
() >= self
._wbufsize
):
246 def writelines(self
, list):
247 # XXX We could do better here for very long lists
248 # XXX Should really reject non-string non-buffers
249 self
._wbuf
.extend(filter(None, map(str, list)))
250 if (self
._wbufsize
<= 1 or
251 self
._get
_wbuf
_len
() >= self
._wbufsize
):
254 def _get_wbuf_len(self
):
260 def read(self
, size
=-1):
268 if self
._rbufsize
<= 1:
269 recv_size
= self
.default_bufsize
271 recv_size
= self
._rbufsize
273 data
= self
._sock
.recv(recv_size
)
277 return "".join(buffers
)
279 # Read until size bytes or EOF seen, whichever comes first
282 self
._rbuf
= data
[size
:]
289 left
= size
- buf_len
290 recv_size
= max(self
._rbufsize
, left
)
291 data
= self
._sock
.recv(recv_size
)
297 self
._rbuf
= data
[left
:]
298 buffers
[-1] = data
[:left
]
301 return "".join(buffers
)
303 def readline(self
, size
=-1):
306 # Read until \n or EOF, whichever comes first
307 if self
._rbufsize
<= 1:
308 # Speed up unbuffered case
311 recv
= self
._sock
.recv
317 return "".join(buffers
)
321 self
._rbuf
= data
[nl
:]
328 data
= self
._sock
.recv(self
._rbufsize
)
335 self
._rbuf
= data
[nl
:]
336 buffers
[-1] = data
[:nl
]
338 return "".join(buffers
)
340 # Read until size bytes or \n or EOF seen, whichever comes first
341 nl
= data
.find('\n', 0, size
)
344 self
._rbuf
= data
[nl
:]
348 self
._rbuf
= data
[size
:]
355 data
= self
._sock
.recv(self
._rbufsize
)
359 left
= size
- buf_len
360 nl
= data
.find('\n', 0, left
)
363 self
._rbuf
= data
[nl
:]
364 buffers
[-1] = data
[:nl
]
368 self
._rbuf
= data
[left
:]
369 buffers
[-1] = data
[:left
]
372 return "".join(buffers
)
374 def readlines(self
, sizehint
=0):
378 line
= self
.readline()
383 if sizehint
and total
>= sizehint
:
393 line
= self
.readline()