PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/3rdParty/SlonEngine/3rdParty/Python26/Lib/socket.py

https://bitbucket.org/DikobrAz/motioneditor
Python | 512 lines | 493 code | 2 blank | 17 comment | 1 complexity | 9a1ae166453627fcc98fc895302e2aee MD5 | raw file
Possible License(s): LGPL-2.1
  1. # Wrapper module for _socket, providing some additional facilities
  2. # implemented in Python.
  3. """\
  4. This module provides socket operations and some related functions.
  5. On Unix, it supports IP (Internet Protocol) and Unix domain sockets.
  6. On other systems, it only supports IP. Functions specific for a
  7. socket are available as methods of the socket object.
  8. Functions:
  9. socket() -- create a new socket object
  10. socketpair() -- create a pair of new socket objects [*]
  11. fromfd() -- create a socket object from an open file descriptor [*]
  12. gethostname() -- return the current hostname
  13. gethostbyname() -- map a hostname to its IP number
  14. gethostbyaddr() -- map an IP number or hostname to DNS info
  15. getservbyname() -- map a service name and a protocol name to a port number
  16. getprotobyname() -- mape a protocol name (e.g. 'tcp') to a number
  17. ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
  18. htons(), htonl() -- convert 16, 32 bit int from host to network byte order
  19. inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format
  20. inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)
  21. ssl() -- secure socket layer support (only available if configured)
  22. socket.getdefaulttimeout() -- get the default timeout value
  23. socket.setdefaulttimeout() -- set the default timeout value
  24. create_connection() -- connects to an address, with an optional timeout
  25. [*] not available on all platforms!
  26. Special objects:
  27. SocketType -- type object for socket objects
  28. error -- exception raised for I/O errors
  29. has_ipv6 -- boolean value indicating if IPv6 is supported
  30. Integer constants:
  31. AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
  32. SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
  33. Many other constants may be defined; these may be used in calls to
  34. the setsockopt() and getsockopt() methods.
  35. """
  36. import _socket
  37. from _socket import *
  38. try:
  39. import _ssl
  40. except ImportError:
  41. # no SSL support
  42. pass
  43. else:
  44. def ssl(sock, keyfile=None, certfile=None):
  45. # we do an internal import here because the ssl
  46. # module imports the socket module
  47. import ssl as _realssl
  48. warnings.warn("socket.ssl() is deprecated. Use ssl.wrap_socket() instead.",
  49. DeprecationWarning, stacklevel=2)
  50. return _realssl.sslwrap_simple(sock, keyfile, certfile)
  51. # we need to import the same constants we used to...
  52. from _ssl import SSLError as sslerror
  53. from _ssl import \
  54. RAND_add, \
  55. RAND_egd, \
  56. RAND_status, \
  57. SSL_ERROR_ZERO_RETURN, \
  58. SSL_ERROR_WANT_READ, \
  59. SSL_ERROR_WANT_WRITE, \
  60. SSL_ERROR_WANT_X509_LOOKUP, \
  61. SSL_ERROR_SYSCALL, \
  62. SSL_ERROR_SSL, \
  63. SSL_ERROR_WANT_CONNECT, \
  64. SSL_ERROR_EOF, \
  65. SSL_ERROR_INVALID_ERROR_CODE
  66. import os, sys, warnings
  67. try:
  68. from cStringIO import StringIO
  69. except ImportError:
  70. from StringIO import StringIO
  71. try:
  72. from errno import EBADF
  73. except ImportError:
  74. EBADF = 9
  75. __all__ = ["getfqdn"]
  76. __all__.extend(os._get_exports_list(_socket))
  77. _realsocket = socket
  78. # WSA error codes
  79. if sys.platform.lower().startswith("win"):
  80. errorTab = {}
  81. errorTab[10004] = "The operation was interrupted."
  82. errorTab[10009] = "A bad file handle was passed."
  83. errorTab[10013] = "Permission denied."
  84. errorTab[10014] = "A fault occurred on the network??" # WSAEFAULT
  85. errorTab[10022] = "An invalid operation was attempted."
  86. errorTab[10035] = "The socket operation would block"
  87. errorTab[10036] = "A blocking operation is already in progress."
  88. errorTab[10048] = "The network address is in use."
  89. errorTab[10054] = "The connection has been reset."
  90. errorTab[10058] = "The network has been shut down."
  91. errorTab[10060] = "The operation timed out."
  92. errorTab[10061] = "Connection refused."
  93. errorTab[10063] = "The name is too long."
  94. errorTab[10064] = "The host is down."
  95. errorTab[10065] = "The host is unreachable."
  96. __all__.append("errorTab")
  97. def getfqdn(name=''):
  98. """Get fully qualified domain name from name.
  99. An empty argument is interpreted as meaning the local host.
  100. First the hostname returned by gethostbyaddr() is checked, then
  101. possibly existing aliases. In case no FQDN is available, hostname
  102. from gethostname() is returned.
  103. """
  104. name = name.strip()
  105. if not name or name == '0.0.0.0':
  106. name = gethostname()
  107. try:
  108. hostname, aliases, ipaddrs = gethostbyaddr(name)
  109. except error:
  110. pass
  111. else:
  112. aliases.insert(0, hostname)
  113. for name in aliases:
  114. if '.' in name:
  115. break
  116. else:
  117. name = hostname
  118. return name
  119. _socketmethods = (
  120. 'bind', 'connect', 'connect_ex', 'fileno', 'listen',
  121. 'getpeername', 'getsockname', 'getsockopt', 'setsockopt',
  122. 'sendall', 'setblocking',
  123. 'settimeout', 'gettimeout', 'shutdown')
  124. if os.name == "nt":
  125. _socketmethods = _socketmethods + ('ioctl',)
  126. if sys.platform == "riscos":
  127. _socketmethods = _socketmethods + ('sleeptaskw',)
  128. # All the method names that must be delegated to either the real socket
  129. # object or the _closedsocket object.
  130. _delegate_methods = ("recv", "recvfrom", "recv_into", "recvfrom_into",
  131. "send", "sendto")
  132. class _closedsocket(object):
  133. __slots__ = []
  134. def _dummy(*args):
  135. raise error(EBADF, 'Bad file descriptor')
  136. # All _delegate_methods must also be initialized here.
  137. send = recv = recv_into = sendto = recvfrom = recvfrom_into = _dummy
  138. __getattr__ = _dummy
  139. # Wrapper around platform socket objects. This implements
  140. # a platform-independent dup() functionality. The
  141. # implementation currently relies on reference counting
  142. # to close the underlying socket object.
  143. class _socketobject(object):
  144. __doc__ = _realsocket.__doc__
  145. __slots__ = ["_sock", "__weakref__"] + list(_delegate_methods)
  146. def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, _sock=None):
  147. if _sock is None:
  148. _sock = _realsocket(family, type, proto)
  149. self._sock = _sock
  150. for method in _delegate_methods:
  151. setattr(self, method, getattr(_sock, method))
  152. def close(self):
  153. self._sock = _closedsocket()
  154. dummy = self._sock._dummy
  155. for method in _delegate_methods:
  156. setattr(self, method, dummy)
  157. close.__doc__ = _realsocket.close.__doc__
  158. def accept(self):
  159. sock, addr = self._sock.accept()
  160. return _socketobject(_sock=sock), addr
  161. accept.__doc__ = _realsocket.accept.__doc__
  162. def dup(self):
  163. """dup() -> socket object
  164. Return a new socket object connected to the same system resource."""
  165. return _socketobject(_sock=self._sock)
  166. def makefile(self, mode='r', bufsize=-1):
  167. """makefile([mode[, bufsize]]) -> file object
  168. Return a regular file object corresponding to the socket. The mode
  169. and bufsize arguments are as for the built-in open() function."""
  170. return _fileobject(self._sock, mode, bufsize)
  171. family = property(lambda self: self._sock.family, doc="the socket family")
  172. type = property(lambda self: self._sock.type, doc="the socket type")
  173. proto = property(lambda self: self._sock.proto, doc="the socket protocol")
  174. _s = ("def %s(self, *args): return self._sock.%s(*args)\n\n"
  175. "%s.__doc__ = _realsocket.%s.__doc__\n")
  176. for _m in _socketmethods:
  177. exec _s % (_m, _m, _m, _m)
  178. del _m, _s
  179. socket = SocketType = _socketobject
  180. class _fileobject(object):
  181. """Faux file object attached to a socket object."""
  182. default_bufsize = 8192
  183. name = "<socket>"
  184. __slots__ = ["mode", "bufsize", "softspace",
  185. # "closed" is a property, see below
  186. "_sock", "_rbufsize", "_wbufsize", "_rbuf", "_wbuf",
  187. "_close"]
  188. def __init__(self, sock, mode='rb', bufsize=-1, close=False):
  189. self._sock = sock
  190. self.mode = mode # Not actually used in this version
  191. if bufsize < 0:
  192. bufsize = self.default_bufsize
  193. self.bufsize = bufsize
  194. self.softspace = False
  195. # _rbufsize is the suggested recv buffer size. It is *strictly*
  196. # obeyed within readline() for recv calls. If it is larger than
  197. # default_bufsize it will be used for recv calls within read().
  198. if bufsize == 0:
  199. self._rbufsize = 1
  200. elif bufsize == 1:
  201. self._rbufsize = self.default_bufsize
  202. else:
  203. self._rbufsize = bufsize
  204. self._wbufsize = bufsize
  205. # We use StringIO for the read buffer to avoid holding a list
  206. # of variously sized string objects which have been known to
  207. # fragment the heap due to how they are malloc()ed and often
  208. # realloc()ed down much smaller than their original allocation.
  209. self._rbuf = StringIO()
  210. self._wbuf = [] # A list of strings
  211. self._close = close
  212. def _getclosed(self):
  213. return self._sock is None
  214. closed = property(_getclosed, doc="True if the file is closed")
  215. def close(self):
  216. try:
  217. if self._sock:
  218. self.flush()
  219. finally:
  220. if self._close:
  221. self._sock.close()
  222. self._sock = None
  223. def __del__(self):
  224. try:
  225. self.close()
  226. except:
  227. # close() may fail if __init__ didn't complete
  228. pass
  229. def flush(self):
  230. if self._wbuf:
  231. buffer = "".join(self._wbuf)
  232. self._wbuf = []
  233. self._sock.sendall(buffer)
  234. def fileno(self):
  235. return self._sock.fileno()
  236. def write(self, data):
  237. data = str(data) # XXX Should really reject non-string non-buffers
  238. if not data:
  239. return
  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):
  244. self.flush()
  245. def writelines(self, list):
  246. # XXX We could do better here for very long lists
  247. # XXX Should really reject non-string non-buffers
  248. self._wbuf.extend(filter(None, map(str, list)))
  249. if (self._wbufsize <= 1 or
  250. self._get_wbuf_len() >= self._wbufsize):
  251. self.flush()
  252. def _get_wbuf_len(self):
  253. buf_len = 0
  254. for x in self._wbuf:
  255. buf_len += len(x)
  256. return buf_len
  257. def read(self, size=-1):
  258. # Use max, disallow tiny reads in a loop as they are very inefficient.
  259. # We never leave read() with any leftover data from a new recv() call
  260. # in our internal buffer.
  261. rbufsize = max(self._rbufsize, self.default_bufsize)
  262. # Our use of StringIO rather than lists of string objects returned by
  263. # recv() minimizes memory usage and fragmentation that occurs when
  264. # rbufsize is large compared to the typical return value of recv().
  265. buf = self._rbuf
  266. buf.seek(0, 2) # seek end
  267. if size < 0:
  268. # Read until EOF
  269. self._rbuf = StringIO() # reset _rbuf. we consume it via buf.
  270. while True:
  271. data = self._sock.recv(rbufsize)
  272. if not data:
  273. break
  274. buf.write(data)
  275. return buf.getvalue()
  276. else:
  277. # Read until size bytes or EOF seen, whichever comes first
  278. buf_len = buf.tell()
  279. if buf_len >= size:
  280. # Already have size bytes in our buffer? Extract and return.
  281. buf.seek(0)
  282. rv = buf.read(size)
  283. self._rbuf = StringIO()
  284. self._rbuf.write(buf.read())
  285. return rv
  286. self._rbuf = StringIO() # reset _rbuf. we consume it via buf.
  287. while True:
  288. left = size - buf_len
  289. # recv() will malloc the amount of memory given as its
  290. # parameter even though it often returns much less data
  291. # than that. The returned data string is short lived
  292. # as we copy it into a StringIO and free it. This avoids
  293. # fragmentation issues on many platforms.
  294. data = self._sock.recv(left)
  295. if not data:
  296. break
  297. n = len(data)
  298. if n == size and not buf_len:
  299. # Shortcut. Avoid buffer data copies when:
  300. # - We have no data in our buffer.
  301. # AND
  302. # - Our call to recv returned exactly the
  303. # number of bytes we were asked to read.
  304. return data
  305. if n == left:
  306. buf.write(data)
  307. del data # explicit free
  308. break
  309. assert n <= left, "recv(%d) returned %d bytes" % (left, n)
  310. buf.write(data)
  311. buf_len += n
  312. del data # explicit free
  313. #assert buf_len == buf.tell()
  314. return buf.getvalue()
  315. def readline(self, size=-1):
  316. buf = self._rbuf
  317. buf.seek(0, 2) # seek end
  318. if buf.tell() > 0:
  319. # check if we already have it in our buffer
  320. buf.seek(0)
  321. bline = buf.readline(size)
  322. if bline.endswith('\n') or len(bline) == size:
  323. self._rbuf = StringIO()
  324. self._rbuf.write(buf.read())
  325. return bline
  326. del bline
  327. if size < 0:
  328. # Read until \n or EOF, whichever comes first
  329. if self._rbufsize <= 1:
  330. # Speed up unbuffered case
  331. buf.seek(0)
  332. buffers = [buf.read()]
  333. self._rbuf = StringIO() # reset _rbuf. we consume it via buf.
  334. data = None
  335. recv = self._sock.recv
  336. while data != "\n":
  337. data = recv(1)
  338. if not data:
  339. break
  340. buffers.append(data)
  341. return "".join(buffers)
  342. buf.seek(0, 2) # seek end
  343. self._rbuf = StringIO() # reset _rbuf. we consume it via buf.
  344. while True:
  345. data = self._sock.recv(self._rbufsize)
  346. if not data:
  347. break
  348. nl = data.find('\n')
  349. if nl >= 0:
  350. nl += 1
  351. buf.write(data[:nl])
  352. self._rbuf.write(data[nl:])
  353. del data
  354. break
  355. buf.write(data)
  356. return buf.getvalue()
  357. else:
  358. # Read until size bytes or \n or EOF seen, whichever comes first
  359. buf.seek(0, 2) # seek end
  360. buf_len = buf.tell()
  361. if buf_len >= size:
  362. buf.seek(0)
  363. rv = buf.read(size)
  364. self._rbuf = StringIO()
  365. self._rbuf.write(buf.read())
  366. return rv
  367. self._rbuf = StringIO() # reset _rbuf. we consume it via buf.
  368. while True:
  369. data = self._sock.recv(self._rbufsize)
  370. if not data:
  371. break
  372. left = size - buf_len
  373. # did we just receive a newline?
  374. nl = data.find('\n', 0, left)
  375. if nl >= 0:
  376. nl += 1
  377. # save the excess data to _rbuf
  378. self._rbuf.write(data[nl:])
  379. if buf_len:
  380. buf.write(data[:nl])
  381. break
  382. else:
  383. # Shortcut. Avoid data copy through buf when returning
  384. # a substring of our first recv().
  385. return data[:nl]
  386. n = len(data)
  387. if n == size and not buf_len:
  388. # Shortcut. Avoid data copy through buf when
  389. # returning exactly all of our first recv().
  390. return data
  391. if n >= left:
  392. buf.write(data[:left])
  393. self._rbuf.write(data[left:])
  394. break
  395. buf.write(data)
  396. buf_len += n
  397. #assert buf_len == buf.tell()
  398. return buf.getvalue()
  399. def readlines(self, sizehint=0):
  400. total = 0
  401. list = []
  402. while True:
  403. line = self.readline()
  404. if not line:
  405. break
  406. list.append(line)
  407. total += len(line)
  408. if sizehint and total >= sizehint:
  409. break
  410. return list
  411. # Iterator protocols
  412. def __iter__(self):
  413. return self
  414. def next(self):
  415. line = self.readline()
  416. if not line:
  417. raise StopIteration
  418. return line
  419. _GLOBAL_DEFAULT_TIMEOUT = object()
  420. def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT):
  421. """Connect to *address* and return the socket object.
  422. Convenience function. Connect to *address* (a 2-tuple ``(host,
  423. port)``) and return the socket object. Passing the optional
  424. *timeout* parameter will set the timeout on the socket instance
  425. before attempting to connect. If no *timeout* is supplied, the
  426. global default timeout setting returned by :func:`getdefaulttimeout`
  427. is used.
  428. """
  429. msg = "getaddrinfo returns an empty list"
  430. host, port = address
  431. for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  432. af, socktype, proto, canonname, sa = res
  433. sock = None
  434. try:
  435. sock = socket(af, socktype, proto)
  436. if timeout is not _GLOBAL_DEFAULT_TIMEOUT:
  437. sock.settimeout(timeout)
  438. sock.connect(sa)
  439. return sock
  440. except error, msg:
  441. if sock is not None:
  442. sock.close()
  443. raise error, msg