2000-08-16 11:14:32 -03:00
|
|
|
# Wrapper module for _socket, providing some additional facilities
|
|
|
|
# implemented in Python.
|
|
|
|
|
|
|
|
"""\
|
|
|
|
This module provides socket operations and some related functions.
|
|
|
|
On Unix, it supports IP (Internet Protocol) and Unix domain sockets.
|
2001-01-14 21:36:40 -04:00
|
|
|
On other systems, it only supports IP. Functions specific for a
|
2000-09-30 08:34:30 -03:00
|
|
|
socket are available as methods of the socket object.
|
2000-08-16 11:14:32 -03:00
|
|
|
|
|
|
|
Functions:
|
|
|
|
|
|
|
|
socket() -- create a new socket object
|
|
|
|
fromfd() -- create a socket object from an open file descriptor [*]
|
|
|
|
gethostname() -- return the current hostname
|
|
|
|
gethostbyname() -- map a hostname to its IP number
|
|
|
|
gethostbyaddr() -- map an IP number or hostname to DNS info
|
|
|
|
getservbyname() -- map a service name and a protocol name to a port number
|
|
|
|
getprotobyname() -- mape a protocol name (e.g. 'tcp') to a number
|
|
|
|
ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
|
|
|
|
htons(), htonl() -- convert 16, 32 bit int from host to network byte order
|
|
|
|
inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format
|
|
|
|
inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)
|
|
|
|
ssl() -- secure socket layer support (only available if configured)
|
2002-07-18 14:08:35 -03:00
|
|
|
socket.getdefaulttimeout() -- get the default timeout value
|
|
|
|
socket.setdefaulttimeout() -- set the default timeout value
|
2000-08-16 11:14:32 -03:00
|
|
|
|
|
|
|
[*] not available on all platforms!
|
|
|
|
|
|
|
|
Special objects:
|
|
|
|
|
|
|
|
SocketType -- type object for socket objects
|
|
|
|
error -- exception raised for I/O errors
|
2003-04-25 02:48:32 -03:00
|
|
|
has_ipv6 -- boolean value indicating if IPv6 is supported
|
2000-08-16 11:14:32 -03:00
|
|
|
|
|
|
|
Integer constants:
|
|
|
|
|
|
|
|
AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
|
|
|
|
SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
|
|
|
|
|
|
|
|
Many other constants may be defined; these may be used in calls to
|
|
|
|
the setsockopt() and getsockopt() methods.
|
|
|
|
"""
|
|
|
|
|
2002-02-17 00:25:24 -04:00
|
|
|
import _socket
|
2000-08-16 11:14:32 -03:00
|
|
|
from _socket import *
|
2002-02-17 00:25:24 -04:00
|
|
|
|
2002-08-08 12:16:20 -03:00
|
|
|
_have_ssl = False
|
2002-02-16 14:23:30 -04:00
|
|
|
try:
|
2002-08-08 12:25:28 -03:00
|
|
|
import _ssl
|
2002-02-16 14:23:30 -04:00
|
|
|
from _ssl import *
|
2002-08-08 12:16:20 -03:00
|
|
|
_have_ssl = True
|
2002-02-16 14:23:30 -04:00
|
|
|
except ImportError:
|
2002-08-08 12:16:20 -03:00
|
|
|
pass
|
2000-08-16 11:14:32 -03:00
|
|
|
|
|
|
|
import os, sys
|
|
|
|
|
2003-04-29 16:50:25 -03:00
|
|
|
try:
|
|
|
|
from errno import EBADF
|
|
|
|
except ImportError:
|
|
|
|
EBADF = 9
|
|
|
|
|
2001-02-15 18:15:14 -04:00
|
|
|
__all__ = ["getfqdn"]
|
|
|
|
__all__.extend(os._get_exports_list(_socket))
|
2002-08-08 12:25:28 -03:00
|
|
|
if _have_ssl:
|
|
|
|
__all__.extend(os._get_exports_list(_ssl))
|
2001-02-15 18:15:14 -04:00
|
|
|
|
2002-08-08 12:16:20 -03:00
|
|
|
_realsocket = socket
|
2000-08-16 11:14:32 -03:00
|
|
|
if (sys.platform.lower().startswith("win")
|
2001-03-02 02:43:49 -04:00
|
|
|
or (hasattr(os, 'uname') and os.uname()[0] == "BeOS")
|
2002-02-17 00:25:24 -04:00
|
|
|
or sys.platform=="riscos"):
|
2000-08-16 11:14:32 -03:00
|
|
|
|
2002-08-08 12:16:20 -03:00
|
|
|
if _have_ssl:
|
|
|
|
_realssl = ssl
|
2001-03-22 18:12:17 -04:00
|
|
|
def ssl(sock, keyfile=None, certfile=None):
|
|
|
|
if hasattr(sock, "_sock"):
|
|
|
|
sock = sock._sock
|
2002-08-08 12:16:20 -03:00
|
|
|
return _realssl(sock, keyfile, certfile)
|
2000-08-16 11:14:32 -03:00
|
|
|
|
|
|
|
# WSA error codes
|
|
|
|
if sys.platform.lower().startswith("win"):
|
|
|
|
errorTab = {}
|
|
|
|
errorTab[10004] = "The operation was interrupted."
|
|
|
|
errorTab[10009] = "A bad file handle was passed."
|
|
|
|
errorTab[10013] = "Permission denied."
|
|
|
|
errorTab[10014] = "A fault occurred on the network??" # WSAEFAULT
|
|
|
|
errorTab[10022] = "An invalid operation was attempted."
|
|
|
|
errorTab[10035] = "The socket operation would block"
|
|
|
|
errorTab[10036] = "A blocking operation is already in progress."
|
|
|
|
errorTab[10048] = "The network address is in use."
|
|
|
|
errorTab[10054] = "The connection has been reset."
|
|
|
|
errorTab[10058] = "The network has been shut down."
|
|
|
|
errorTab[10060] = "The operation timed out."
|
|
|
|
errorTab[10061] = "Connection refused."
|
|
|
|
errorTab[10063] = "The name is too long."
|
|
|
|
errorTab[10064] = "The host is down."
|
|
|
|
errorTab[10065] = "The host is unreachable."
|
2001-03-18 15:53:21 -04:00
|
|
|
__all__.append("errorTab")
|
2002-08-08 12:25:28 -03:00
|
|
|
|
2000-08-16 11:14:32 -03:00
|
|
|
del os, sys
|
|
|
|
|
|
|
|
|
|
|
|
def getfqdn(name=''):
|
|
|
|
"""Get fully qualified domain name from name.
|
|
|
|
|
|
|
|
An empty argument is interpreted as meaning the local host.
|
|
|
|
|
|
|
|
First the hostname returned by gethostbyaddr() is checked, then
|
|
|
|
possibly existing aliases. In case no FQDN is available, hostname
|
|
|
|
is returned.
|
|
|
|
"""
|
|
|
|
name = name.strip()
|
2000-08-16 17:30:21 -03:00
|
|
|
if not name or name == '0.0.0.0':
|
2000-08-16 11:14:32 -03:00
|
|
|
name = gethostname()
|
|
|
|
try:
|
|
|
|
hostname, aliases, ipaddrs = gethostbyaddr(name)
|
|
|
|
except error:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
aliases.insert(0, hostname)
|
|
|
|
for name in aliases:
|
|
|
|
if '.' in name:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
name = hostname
|
|
|
|
return name
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# These classes are used by the socket() defined on Windows and BeOS
|
|
|
|
# platforms to provide a best-effort implementation of the cleanup
|
|
|
|
# semantics needed when sockets can't be dup()ed.
|
|
|
|
#
|
|
|
|
# These are not actually used on other platforms.
|
|
|
|
#
|
|
|
|
|
2001-08-17 22:23:20 -03:00
|
|
|
_socketmethods = (
|
|
|
|
'bind', 'connect', 'connect_ex', 'fileno', 'listen',
|
|
|
|
'getpeername', 'getsockname', 'getsockopt', 'setsockopt',
|
2003-04-29 16:27:26 -03:00
|
|
|
'sendall', 'setblocking',
|
2002-06-06 18:08:16 -03:00
|
|
|
'settimeout', 'gettimeout', 'shutdown')
|
2001-08-17 22:23:20 -03:00
|
|
|
|
2002-08-08 15:11:36 -03:00
|
|
|
class _closedsocket(object):
|
|
|
|
__slots__ = []
|
2003-04-29 16:27:26 -03:00
|
|
|
def _dummy(*args):
|
2003-04-29 16:50:25 -03:00
|
|
|
raise error(EBADF, 'Bad file descriptor')
|
2003-04-29 16:27:26 -03:00
|
|
|
send = recv = sendto = recvfrom = __getattr__ = _dummy
|
2002-08-08 15:11:36 -03:00
|
|
|
|
2002-08-08 12:16:20 -03:00
|
|
|
class _socketobject(object):
|
|
|
|
|
|
|
|
__doc__ = _realsocket.__doc__
|
2000-08-16 11:14:32 -03:00
|
|
|
|
2003-04-29 16:27:26 -03:00
|
|
|
__slots__ = ["_sock", "send", "recv", "sendto", "recvfrom"]
|
2002-08-08 12:16:20 -03:00
|
|
|
|
|
|
|
def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, _sock=None):
|
|
|
|
if _sock is None:
|
2002-08-08 12:22:12 -03:00
|
|
|
_sock = _realsocket(family, type, proto)
|
2002-08-08 12:16:20 -03:00
|
|
|
self._sock = _sock
|
2003-04-29 16:27:26 -03:00
|
|
|
self.send = self._sock.send
|
|
|
|
self.recv = self._sock.recv
|
|
|
|
self.sendto = self._sock.sendto
|
|
|
|
self.recvfrom = self._sock.recvfrom
|
2000-08-16 11:14:32 -03:00
|
|
|
|
|
|
|
def close(self):
|
2002-08-08 15:11:36 -03:00
|
|
|
self._sock = _closedsocket()
|
2003-04-29 16:27:26 -03:00
|
|
|
self.send = self.recv = self.sendto = self.recvfrom = self._sock._dummy
|
2002-08-08 12:16:20 -03:00
|
|
|
close.__doc__ = _realsocket.close.__doc__
|
2000-08-16 11:14:32 -03:00
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
sock, addr = self._sock.accept()
|
2002-08-08 12:16:20 -03:00
|
|
|
return _socketobject(_sock=sock), addr
|
|
|
|
accept.__doc__ = _realsocket.accept.__doc__
|
2000-08-16 11:14:32 -03:00
|
|
|
|
|
|
|
def dup(self):
|
2002-08-08 12:16:20 -03:00
|
|
|
"""dup() -> socket object
|
|
|
|
|
|
|
|
Return a new socket object connected to the same system resource."""
|
|
|
|
return _socketobject(_sock=self._sock)
|
2000-08-16 11:14:32 -03:00
|
|
|
|
|
|
|
def makefile(self, mode='r', bufsize=-1):
|
2002-08-08 12:16:20 -03:00
|
|
|
"""makefile([mode[, bufsize]]) -> file object
|
|
|
|
|
|
|
|
Return a regular file object corresponding to the socket. The mode
|
|
|
|
and bufsize arguments are as for the built-in open() function."""
|
2000-08-16 11:14:32 -03:00
|
|
|
return _fileobject(self._sock, mode, bufsize)
|
|
|
|
|
2002-08-08 12:16:20 -03:00
|
|
|
_s = ("def %s(self, *args): return self._sock.%s(*args)\n\n"
|
|
|
|
"%s.__doc__ = _realsocket.%s.__doc__\n")
|
2001-08-17 22:23:20 -03:00
|
|
|
for _m in _socketmethods:
|
2002-08-08 12:16:20 -03:00
|
|
|
exec _s % (_m, _m, _m, _m)
|
2002-08-08 17:07:03 -03:00
|
|
|
del _m, _s
|
2000-08-16 11:14:32 -03:00
|
|
|
|
2003-03-30 00:54:24 -04:00
|
|
|
socket = SocketType = _socketobject
|
2000-08-16 11:14:32 -03:00
|
|
|
|
2002-08-08 12:16:20 -03:00
|
|
|
class _fileobject(object):
|
2002-08-07 22:02:16 -03:00
|
|
|
"""Faux file object attached to a socket object."""
|
|
|
|
|
|
|
|
default_bufsize = 8192
|
2002-08-08 12:16:20 -03:00
|
|
|
name = "<socket>"
|
|
|
|
|
|
|
|
__slots__ = ["mode", "bufsize", "softspace",
|
|
|
|
# "closed" is a property, see below
|
|
|
|
"_sock", "_rbufsize", "_wbufsize", "_rbuf", "_wbuf"]
|
2000-08-16 11:14:32 -03:00
|
|
|
|
2002-08-07 12:46:19 -03:00
|
|
|
def __init__(self, sock, mode='rb', bufsize=-1):
|
2000-08-16 11:14:32 -03:00
|
|
|
self._sock = sock
|
2002-08-08 12:16:20 -03:00
|
|
|
self.mode = mode # Not actually used in this version
|
2002-08-07 22:02:16 -03:00
|
|
|
if bufsize < 0:
|
|
|
|
bufsize = self.default_bufsize
|
2002-08-08 12:22:12 -03:00
|
|
|
self.bufsize = bufsize
|
|
|
|
self.softspace = False
|
2002-08-07 22:02:16 -03:00
|
|
|
if bufsize == 0:
|
|
|
|
self._rbufsize = 1
|
|
|
|
elif bufsize == 1:
|
|
|
|
self._rbufsize = self.default_bufsize
|
|
|
|
else:
|
|
|
|
self._rbufsize = bufsize
|
2000-08-16 11:14:32 -03:00
|
|
|
self._wbufsize = bufsize
|
2002-08-08 14:16:09 -03:00
|
|
|
self._rbuf = "" # A string
|
|
|
|
self._wbuf = [] # A list of strings
|
2000-08-16 11:14:32 -03:00
|
|
|
|
2002-08-08 12:16:20 -03:00
|
|
|
def _getclosed(self):
|
|
|
|
return self._sock is not None
|
|
|
|
closed = property(_getclosed, doc="True if the file is closed")
|
|
|
|
|
2000-08-16 11:14:32 -03:00
|
|
|
def close(self):
|
|
|
|
try:
|
|
|
|
if self._sock:
|
|
|
|
self.flush()
|
|
|
|
finally:
|
2002-06-06 18:08:16 -03:00
|
|
|
self._sock = None
|
2000-08-16 11:14:32 -03:00
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
def flush(self):
|
|
|
|
if self._wbuf:
|
2002-08-07 22:02:16 -03:00
|
|
|
buffer = "".join(self._wbuf)
|
|
|
|
self._wbuf = []
|
2002-06-06 18:08:16 -03:00
|
|
|
self._sock.sendall(buffer)
|
2000-08-16 11:14:32 -03:00
|
|
|
|
2002-06-13 19:18:39 -03:00
|
|
|
def fileno(self):
|
2000-08-16 11:14:32 -03:00
|
|
|
return self._sock.fileno()
|
|
|
|
|
|
|
|
def write(self, data):
|
2002-08-07 22:02:16 -03:00
|
|
|
data = str(data) # XXX Should really reject non-string non-buffers
|
|
|
|
if not data:
|
|
|
|
return
|
|
|
|
self._wbuf.append(data)
|
|
|
|
if (self._wbufsize == 0 or
|
|
|
|
self._wbufsize == 1 and '\n' in data or
|
|
|
|
self._get_wbuf_len() >= self._wbufsize):
|
2002-06-06 18:08:16 -03:00
|
|
|
self.flush()
|
2000-08-16 11:14:32 -03:00
|
|
|
|
|
|
|
def writelines(self, list):
|
2002-08-07 22:02:16 -03:00
|
|
|
# XXX We could do better here for very long lists
|
|
|
|
# XXX Should really reject non-string non-buffers
|
|
|
|
self._wbuf.extend(filter(None, map(str, list)))
|
|
|
|
if (self._wbufsize <= 1 or
|
|
|
|
self._get_wbuf_len() >= self._wbufsize):
|
|
|
|
self.flush()
|
2000-08-16 11:14:32 -03:00
|
|
|
|
2002-08-07 22:02:16 -03:00
|
|
|
def _get_wbuf_len(self):
|
2002-06-06 18:08:16 -03:00
|
|
|
buf_len = 0
|
2002-08-07 22:02:16 -03:00
|
|
|
for x in self._wbuf:
|
|
|
|
buf_len += len(x)
|
2002-06-06 18:08:16 -03:00
|
|
|
return buf_len
|
|
|
|
|
|
|
|
def read(self, size=-1):
|
2002-08-08 14:16:09 -03:00
|
|
|
data = self._rbuf
|
2002-08-07 22:02:16 -03:00
|
|
|
if size < 0:
|
|
|
|
# Read until EOF
|
2002-08-08 14:16:09 -03:00
|
|
|
buffers = []
|
|
|
|
if data:
|
|
|
|
buffers.append(data)
|
|
|
|
self._rbuf = ""
|
2002-08-07 22:02:16 -03:00
|
|
|
if self._rbufsize <= 1:
|
|
|
|
recv_size = self.default_bufsize
|
|
|
|
else:
|
|
|
|
recv_size = self._rbufsize
|
2002-08-08 14:16:09 -03:00
|
|
|
while True:
|
2002-08-07 22:02:16 -03:00
|
|
|
data = self._sock.recv(recv_size)
|
|
|
|
if not data:
|
|
|
|
break
|
2002-08-08 14:16:09 -03:00
|
|
|
buffers.append(data)
|
|
|
|
return "".join(buffers)
|
2002-08-07 22:02:16 -03:00
|
|
|
else:
|
2002-08-08 14:16:09 -03:00
|
|
|
# Read until size bytes or EOF seen, whichever comes first
|
|
|
|
buf_len = len(data)
|
|
|
|
if buf_len >= size:
|
|
|
|
self._rbuf = data[size:]
|
|
|
|
return data[:size]
|
|
|
|
buffers = []
|
|
|
|
if data:
|
|
|
|
buffers.append(data)
|
|
|
|
self._rbuf = ""
|
|
|
|
while True:
|
|
|
|
left = size - buf_len
|
|
|
|
recv_size = max(self._rbufsize, left)
|
2002-08-07 22:02:16 -03:00
|
|
|
data = self._sock.recv(recv_size)
|
|
|
|
if not data:
|
|
|
|
break
|
2002-08-08 14:16:09 -03:00
|
|
|
buffers.append(data)
|
|
|
|
n = len(data)
|
|
|
|
if n >= left:
|
|
|
|
self._rbuf = data[left:]
|
|
|
|
buffers[-1] = data[:left]
|
|
|
|
break
|
|
|
|
buf_len += n
|
|
|
|
return "".join(buffers)
|
2002-06-06 18:08:16 -03:00
|
|
|
|
|
|
|
def readline(self, size=-1):
|
2002-08-08 14:16:09 -03:00
|
|
|
data = self._rbuf
|
|
|
|
if size < 0:
|
|
|
|
# Read until \n or EOF, whichever comes first
|
2002-08-08 14:34:19 -03:00
|
|
|
if self._rbufsize <= 1:
|
|
|
|
# Speed up unbuffered case
|
|
|
|
assert data == ""
|
|
|
|
buffers = []
|
|
|
|
recv = self._sock.recv
|
|
|
|
while data != "\n":
|
|
|
|
data = recv(1)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
buffers.append(data)
|
|
|
|
return "".join(buffers)
|
2002-08-08 14:16:09 -03:00
|
|
|
nl = data.find('\n')
|
|
|
|
if nl >= 0:
|
|
|
|
nl += 1
|
|
|
|
self._rbuf = data[nl:]
|
|
|
|
return data[:nl]
|
|
|
|
buffers = []
|
|
|
|
if data:
|
|
|
|
buffers.append(data)
|
|
|
|
self._rbuf = ""
|
|
|
|
while True:
|
|
|
|
data = self._sock.recv(self._rbufsize)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
buffers.append(data)
|
|
|
|
nl = data.find('\n')
|
|
|
|
if nl >= 0:
|
|
|
|
nl += 1
|
|
|
|
self._rbuf = data[nl:]
|
|
|
|
buffers[-1] = data[:nl]
|
|
|
|
break
|
|
|
|
return "".join(buffers)
|
2002-06-06 18:08:16 -03:00
|
|
|
else:
|
2002-08-08 14:16:09 -03:00
|
|
|
# Read until size bytes or \n or EOF seen, whichever comes first
|
|
|
|
nl = data.find('\n', 0, size)
|
|
|
|
if nl >= 0:
|
|
|
|
nl += 1
|
|
|
|
self._rbuf = data[nl:]
|
|
|
|
return data[:nl]
|
|
|
|
buf_len = len(data)
|
|
|
|
if buf_len >= size:
|
|
|
|
self._rbuf = data[size:]
|
|
|
|
return data[:size]
|
|
|
|
buffers = []
|
|
|
|
if data:
|
|
|
|
buffers.append(data)
|
|
|
|
self._rbuf = ""
|
|
|
|
while True:
|
|
|
|
data = self._sock.recv(self._rbufsize)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
buffers.append(data)
|
|
|
|
left = size - buf_len
|
|
|
|
nl = data.find('\n', 0, left)
|
|
|
|
if nl >= 0:
|
|
|
|
nl += 1
|
|
|
|
self._rbuf = data[nl:]
|
|
|
|
buffers[-1] = data[:nl]
|
|
|
|
break
|
|
|
|
n = len(data)
|
|
|
|
if n >= left:
|
|
|
|
self._rbuf = data[left:]
|
|
|
|
buffers[-1] = data[:left]
|
|
|
|
break
|
|
|
|
buf_len += n
|
|
|
|
return "".join(buffers)
|
2000-08-16 11:14:32 -03:00
|
|
|
|
2002-06-06 18:08:16 -03:00
|
|
|
def readlines(self, sizehint=0):
|
2000-09-19 08:25:58 -03:00
|
|
|
total = 0
|
2000-08-16 11:14:32 -03:00
|
|
|
list = []
|
2002-08-08 14:16:09 -03:00
|
|
|
while True:
|
2000-08-16 11:14:32 -03:00
|
|
|
line = self.readline()
|
2002-06-13 19:18:39 -03:00
|
|
|
if not line:
|
|
|
|
break
|
2000-08-16 11:14:32 -03:00
|
|
|
list.append(line)
|
2000-09-19 08:25:58 -03:00
|
|
|
total += len(line)
|
|
|
|
if sizehint and total >= sizehint:
|
|
|
|
break
|
2000-08-16 11:14:32 -03:00
|
|
|
return list
|
2002-08-07 22:02:16 -03:00
|
|
|
|
|
|
|
# Iterator protocols
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
line = self.readline()
|
|
|
|
if not line:
|
|
|
|
raise StopIteration
|
|
|
|
return line
|