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)
|
|
|
|
|
|
|
|
[*] not available on all platforms!
|
|
|
|
|
|
|
|
Special objects:
|
|
|
|
|
|
|
|
SocketType -- type object for socket objects
|
|
|
|
error -- exception raised for I/O errors
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
SSL_EXISTS = 1
|
2002-02-16 14:23:30 -04:00
|
|
|
try:
|
2002-02-17 00:25:24 -04:00
|
|
|
import _ssl
|
2002-02-16 14:23:30 -04:00
|
|
|
from _ssl import *
|
|
|
|
except ImportError:
|
2002-02-17 00:25:24 -04:00
|
|
|
SSL_EXISTS = 0
|
2000-08-16 11:14:32 -03:00
|
|
|
|
|
|
|
import os, sys
|
|
|
|
|
2001-02-15 18:15:14 -04:00
|
|
|
__all__ = ["getfqdn"]
|
|
|
|
__all__.extend(os._get_exports_list(_socket))
|
2002-02-17 00:25:24 -04:00
|
|
|
# XXX shouldn't there be something similar to the above for _ssl exports?
|
2001-02-15 18:15:14 -04:00
|
|
|
|
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
|
|
|
|
2001-03-22 18:12:17 -04:00
|
|
|
_realsocketcall = _socket.socket
|
2000-08-16 11:14:32 -03:00
|
|
|
|
|
|
|
def socket(family, type, proto=0):
|
|
|
|
return _socketobject(_realsocketcall(family, type, proto))
|
|
|
|
|
2002-02-17 00:25:24 -04:00
|
|
|
if SSL_EXISTS:
|
2002-02-16 14:23:30 -04:00
|
|
|
_realsslcall = _ssl.ssl
|
2001-03-22 18:12:17 -04:00
|
|
|
def ssl(sock, keyfile=None, certfile=None):
|
|
|
|
if hasattr(sock, "_sock"):
|
|
|
|
sock = sock._sock
|
2001-03-29 00:36:09 -04:00
|
|
|
return _realsslcall(sock, keyfile, certfile)
|
2001-03-22 18:12:17 -04:00
|
|
|
|
2002-02-17 00:25:24 -04:00
|
|
|
del _socket
|
|
|
|
if SSL_EXISTS:
|
|
|
|
del _ssl
|
|
|
|
del SSL_EXISTS
|
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")
|
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',
|
2002-06-06 18:08:16 -03:00
|
|
|
'recv', 'recvfrom', 'send', 'sendall', 'sendto', 'setblocking',
|
|
|
|
'settimeout', 'gettimeout', 'shutdown')
|
2001-08-17 22:23:20 -03:00
|
|
|
|
2000-08-16 11:14:32 -03:00
|
|
|
class _socketobject:
|
|
|
|
|
2001-12-18 18:22:25 -04:00
|
|
|
class _closedsocket:
|
|
|
|
def __getattr__(self, name):
|
|
|
|
raise error(9, 'Bad file descriptor')
|
|
|
|
|
2000-08-16 11:14:32 -03:00
|
|
|
def __init__(self, sock):
|
|
|
|
self._sock = sock
|
|
|
|
|
|
|
|
def close(self):
|
2001-12-18 18:22:25 -04:00
|
|
|
# Avoid referencing globals here
|
|
|
|
self._sock = self.__class__._closedsocket()
|
2000-08-16 11:14:32 -03:00
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
sock, addr = self._sock.accept()
|
|
|
|
return _socketobject(sock), addr
|
|
|
|
|
|
|
|
def dup(self):
|
|
|
|
return _socketobject(self._sock)
|
|
|
|
|
|
|
|
def makefile(self, mode='r', bufsize=-1):
|
|
|
|
return _fileobject(self._sock, mode, bufsize)
|
|
|
|
|
2001-08-17 22:23:20 -03:00
|
|
|
_s = "def %s(self, *args): return self._sock.%s(*args)\n\n"
|
|
|
|
for _m in _socketmethods:
|
2000-08-16 11:14:32 -03:00
|
|
|
exec _s % (_m, _m)
|
|
|
|
|
|
|
|
|
|
|
|
class _fileobject:
|
2002-06-06 18:08:16 -03:00
|
|
|
"""Implements a file object on top of a regular socket object."""
|
2000-08-16 11:14:32 -03:00
|
|
|
|
2002-06-06 18:08:16 -03:00
|
|
|
def __init__(self, sock, mode='rb', bufsize=8192):
|
2000-08-16 11:14:32 -03:00
|
|
|
self._sock = sock
|
|
|
|
self._mode = mode
|
2002-06-06 18:08:16 -03:00
|
|
|
if bufsize <= 0:
|
2000-08-16 11:14:32 -03:00
|
|
|
bufsize = 512
|
2002-06-06 18:08:16 -03:00
|
|
|
self._rbufsize = bufsize
|
2000-08-16 11:14:32 -03:00
|
|
|
self._wbufsize = bufsize
|
2002-06-06 18:08:16 -03:00
|
|
|
self._rbuf = [ ]
|
|
|
|
self._wbuf = [ ]
|
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-06-06 18:08:16 -03:00
|
|
|
buffer = ''.join(self._wbuf)
|
|
|
|
self._sock.sendall(buffer)
|
|
|
|
self._wbuf = [ ]
|
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-06-06 18:08:16 -03:00
|
|
|
self._wbuf.append (data)
|
|
|
|
# A _wbufsize of 1 means we're doing unbuffered IO.
|
|
|
|
# Flush accordingly.
|
2000-08-16 11:14:32 -03:00
|
|
|
if self._wbufsize == 1:
|
|
|
|
if '\n' in data:
|
2002-06-06 18:08:16 -03:00
|
|
|
self.flush ()
|
|
|
|
elif self.__get_wbuf_len() >= self._wbufsize:
|
|
|
|
self.flush()
|
2000-08-16 11:14:32 -03:00
|
|
|
|
|
|
|
def writelines(self, list):
|
2002-02-16 19:06:19 -04:00
|
|
|
filter(self._sock.sendall, list)
|
2000-08-16 11:14:32 -03:00
|
|
|
self.flush()
|
|
|
|
|
2002-06-06 18:08:16 -03:00
|
|
|
def __get_wbuf_len (self):
|
|
|
|
buf_len = 0
|
|
|
|
for i in [len(x) for x in self._wbuf]:
|
|
|
|
buf_len += i
|
|
|
|
return buf_len
|
|
|
|
|
|
|
|
def __get_rbuf_len(self):
|
|
|
|
buf_len = 0
|
|
|
|
for i in [len(x) for x in self._rbuf]:
|
|
|
|
buf_len += i
|
|
|
|
return buf_len
|
|
|
|
|
|
|
|
def read(self, size=-1):
|
|
|
|
buf_len = self.__get_rbuf_len()
|
|
|
|
while size < 0 or buf_len < size:
|
|
|
|
recv_size = max(self._rbufsize, size - buf_len)
|
|
|
|
data = self._sock.recv(recv_size)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
buf_len += len(data)
|
|
|
|
self._rbuf.append(data)
|
|
|
|
# Clear the rbuf at the end so we're not affected by
|
|
|
|
# an exception during a recv
|
2002-06-13 19:18:39 -03:00
|
|
|
data = ''.join(self._rbuf)
|
2002-06-06 18:08:16 -03:00
|
|
|
self._rbuf = [ ]
|
|
|
|
if buf_len > size and size >= 0:
|
2002-06-13 19:18:39 -03:00
|
|
|
self._rbuf.append(data[size:])
|
|
|
|
data = data[:size]
|
2002-06-06 18:08:16 -03:00
|
|
|
return data
|
|
|
|
|
|
|
|
def readline(self, size=-1):
|
|
|
|
index = -1
|
|
|
|
buf_len = self.__get_rbuf_len()
|
2002-06-13 19:18:39 -03:00
|
|
|
if self._rbuf:
|
2002-06-06 18:08:16 -03:00
|
|
|
index = min([x.find('\n') for x in self._rbuf])
|
|
|
|
while index < 0 and (size < 0 or buf_len < size):
|
|
|
|
recv_size = max(self._rbufsize, size - buf_len)
|
|
|
|
data = self._sock.recv(recv_size)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
buf_len += len(data)
|
|
|
|
self._rbuf.append(data)
|
|
|
|
index = data.find('\n')
|
|
|
|
data = ''.join(self._rbuf)
|
|
|
|
self._rbuf = [ ]
|
|
|
|
index = data.find('\n')
|
|
|
|
if index >= 0:
|
|
|
|
index += 1
|
|
|
|
elif buf_len > size:
|
|
|
|
index = size
|
|
|
|
else:
|
|
|
|
index = buf_len
|
|
|
|
self._rbuf.append(data[index:])
|
|
|
|
data = data[:index]
|
2000-08-16 11:14:32 -03:00
|
|
|
return data
|
|
|
|
|
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 = []
|
|
|
|
while 1:
|
|
|
|
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
|