469 lines
14 KiB
Python
469 lines
14 KiB
Python
from eventlet.api import trampoline
|
|
from eventlet.hubs import get_hub
|
|
|
|
BUFFER_SIZE = 4096
|
|
|
|
import errno
|
|
import os
|
|
import socket
|
|
from socket import socket as _original_socket
|
|
import sys
|
|
import time
|
|
import warnings
|
|
|
|
|
|
from errno import EWOULDBLOCK, EAGAIN
|
|
|
|
|
|
__all__ = ['GreenSocket', 'GreenPipe', 'shutdown_safe']
|
|
|
|
CONNECT_ERR = set((errno.EINPROGRESS, errno.EALREADY, errno.EWOULDBLOCK))
|
|
CONNECT_SUCCESS = set((0, errno.EISCONN))
|
|
|
|
def socket_connect(descriptor, address):
|
|
err = descriptor.connect_ex(address)
|
|
if err in CONNECT_ERR:
|
|
return None
|
|
if err not in CONNECT_SUCCESS:
|
|
raise socket.error(err, errno.errorcode[err])
|
|
return descriptor
|
|
|
|
|
|
def socket_accept(descriptor):
|
|
try:
|
|
return descriptor.accept()
|
|
except socket.error, e:
|
|
if e[0] == errno.EWOULDBLOCK:
|
|
return None
|
|
raise
|
|
|
|
|
|
if sys.platform[:3]=="win":
|
|
# winsock sometimes throws ENOTCONN
|
|
SOCKET_BLOCKING = set((errno.EWOULDBLOCK,))
|
|
SOCKET_CLOSED = set((errno.ECONNRESET, errno.ENOTCONN, errno.ESHUTDOWN))
|
|
else:
|
|
# oddly, on linux/darwin, an unconnected socket is expected to block,
|
|
# so we treat ENOTCONN the same as EWOULDBLOCK
|
|
SOCKET_BLOCKING = set((errno.EWOULDBLOCK, errno.ENOTCONN))
|
|
SOCKET_CLOSED = set((errno.ECONNRESET, errno.ESHUTDOWN, errno.EPIPE))
|
|
|
|
|
|
def set_nonblocking(fd):
|
|
try:
|
|
setblocking = fd.setblocking
|
|
except AttributeError:
|
|
# This version of Python predates socket.setblocking()
|
|
import fcntl
|
|
fileno = fd.fileno()
|
|
flags = fcntl.fcntl(fileno, fcntl.F_GETFL)
|
|
fcntl.fcntl(fileno, fcntl.F_SETFL, flags | os.O_NONBLOCK)
|
|
else:
|
|
# socket supports setblocking()
|
|
setblocking(0)
|
|
|
|
|
|
try:
|
|
from socket import _GLOBAL_DEFAULT_TIMEOUT
|
|
except ImportError:
|
|
_GLOBAL_DEFAULT_TIMEOUT = object()
|
|
|
|
|
|
class GreenSocket(object):
|
|
timeout = None
|
|
def __init__(self, family_or_realsock=socket.AF_INET, *args, **kwargs):
|
|
if isinstance(family_or_realsock, (int, long)):
|
|
fd = _original_socket(family_or_realsock, *args, **kwargs)
|
|
else:
|
|
fd = family_or_realsock
|
|
assert not args, args
|
|
assert not kwargs, kwargs
|
|
try:
|
|
orig_timeout = fd.gettimeout()
|
|
except AttributeError:
|
|
orig_timeout = None
|
|
|
|
set_nonblocking(fd)
|
|
self.fd = fd
|
|
self._fileno = fd.fileno()
|
|
self.closed = False
|
|
self.timeout = socket.getdefaulttimeout()
|
|
|
|
# when client calls setblocking(0) or settimeout(0) the socket must
|
|
# act non-blocking
|
|
self.act_non_blocking = False
|
|
|
|
# import timeout from the other fd if it's distinct
|
|
if orig_timeout and orig_timeout is not self.timeout:
|
|
self.settimeout(orig_timeout)
|
|
|
|
@property
|
|
def _sock(self):
|
|
return self
|
|
|
|
@property
|
|
def family(self):
|
|
return self.fd.family
|
|
|
|
@property
|
|
def type(self):
|
|
return self.fd.type
|
|
|
|
@property
|
|
def proto(self):
|
|
return self.fd.proto
|
|
|
|
def accept(self):
|
|
if self.act_non_blocking:
|
|
return self.fd.accept()
|
|
fd = self.fd
|
|
while True:
|
|
res = socket_accept(fd)
|
|
if res is not None:
|
|
client, addr = res
|
|
set_nonblocking(client)
|
|
return type(self)(client), addr
|
|
trampoline(fd, read=True, timeout=self.gettimeout(),
|
|
timeout_exc=socket.timeout)
|
|
|
|
def bind(self, *args, **kw):
|
|
fn = self.bind = self.fd.bind
|
|
return fn(*args, **kw)
|
|
|
|
def close(self, *args, **kw):
|
|
if self.closed:
|
|
return
|
|
self.closed = True
|
|
fileno = self.fileno()
|
|
try:
|
|
res = self.fd.close()
|
|
finally:
|
|
get_hub().closed(fileno)
|
|
return res
|
|
|
|
def connect(self, address):
|
|
if self.act_non_blocking:
|
|
return self.fd.connect(address)
|
|
fd = self.fd
|
|
if self.gettimeout() is None:
|
|
while not socket_connect(fd, address):
|
|
trampoline(fd, write=True, timeout_exc=socket.timeout)
|
|
else:
|
|
end = time.time() + self.gettimeout()
|
|
while True:
|
|
if socket_connect(fd, address):
|
|
return
|
|
if time.time() >= end:
|
|
raise socket.timeout
|
|
trampoline(fd, write=True, timeout=end-time.time(), timeout_exc=socket.timeout)
|
|
|
|
def connect_ex(self, address):
|
|
if self.act_non_blocking:
|
|
return self.fd.connect_ex(address)
|
|
fd = self.fd
|
|
if self.gettimeout() is None:
|
|
while not socket_connect(fd, address):
|
|
try:
|
|
trampoline(fd, write=True, timeout_exc=socket.timeout)
|
|
except socket.error, ex:
|
|
return ex[0]
|
|
else:
|
|
end = time.time() + self.gettimeout()
|
|
while True:
|
|
if socket_connect(fd, address):
|
|
return 0
|
|
if time.time() >= end:
|
|
raise socket.timeout
|
|
try:
|
|
trampoline(fd, write=True, timeout=end-time.time(), timeout_exc=socket.timeout)
|
|
except socket.error, ex:
|
|
return ex[0]
|
|
|
|
def dup(self, *args, **kw):
|
|
sock = self.fd.dup(*args, **kw)
|
|
set_nonblocking(sock)
|
|
newsock = type(self)(sock)
|
|
newsock.settimeout(self.timeout)
|
|
return newsock
|
|
|
|
def fileno(self, *args, **kw):
|
|
fn = self.fileno = self.fd.fileno
|
|
return fn(*args, **kw)
|
|
|
|
def getpeername(self, *args, **kw):
|
|
fn = self.getpeername = self.fd.getpeername
|
|
return fn(*args, **kw)
|
|
|
|
def getsockname(self, *args, **kw):
|
|
fn = self.getsockname = self.fd.getsockname
|
|
return fn(*args, **kw)
|
|
|
|
def getsockopt(self, *args, **kw):
|
|
fn = self.getsockopt = self.fd.getsockopt
|
|
return fn(*args, **kw)
|
|
|
|
def listen(self, *args, **kw):
|
|
fn = self.listen = self.fd.listen
|
|
return fn(*args, **kw)
|
|
|
|
def makefile(self, mode='r', bufsize=-1):
|
|
return socket._fileobject(self.dup(), mode, bufsize)
|
|
|
|
def makeGreenFile(self, mode='r', bufsize=-1):
|
|
warnings.warn("makeGreenFile has been deprecated, please use "
|
|
"makefile instead", DeprecationWarning, stacklevel=2)
|
|
return self.makefile(mode, bufsize)
|
|
|
|
def recv(self, buflen, flags=0):
|
|
fd = self.fd
|
|
if self.act_non_blocking:
|
|
return fd.recv(buflen, flags)
|
|
while True:
|
|
try:
|
|
return fd.recv(buflen, flags)
|
|
except socket.error, e:
|
|
if e[0] in SOCKET_BLOCKING:
|
|
pass
|
|
elif e[0] in SOCKET_CLOSED:
|
|
return ''
|
|
else:
|
|
raise
|
|
trampoline(fd,
|
|
read=True,
|
|
timeout=self.gettimeout(),
|
|
timeout_exc=socket.timeout)
|
|
|
|
def recvfrom(self, *args):
|
|
if not self.act_non_blocking:
|
|
trampoline(self.fd, read=True, timeout=self.gettimeout(), timeout_exc=socket.timeout)
|
|
return self.fd.recvfrom(*args)
|
|
|
|
def recvfrom_into(self, *args):
|
|
if not self.act_non_blocking:
|
|
trampoline(self.fd, read=True, timeout=self.gettimeout(), timeout_exc=socket.timeout)
|
|
return self.fd.recvfrom_into(*args)
|
|
|
|
def recv_into(self, *args):
|
|
if not self.act_non_blocking:
|
|
trampoline(self.fd, read=True, timeout=self.gettimeout(), timeout_exc=socket.timeout)
|
|
return self.fd.recv_into(*args)
|
|
|
|
def send(self, data, flags=0):
|
|
fd = self.fd
|
|
if self.act_non_blocking:
|
|
return fd.send(data, flags)
|
|
try:
|
|
return fd.send(data, flags)
|
|
except socket.error, e:
|
|
if e[0] in SOCKET_BLOCKING:
|
|
return 0
|
|
raise
|
|
|
|
def sendall(self, data, flags=0):
|
|
fd = self.fd
|
|
tail = self.send(data, flags)
|
|
while tail < len(data):
|
|
trampoline(fd,
|
|
write=True,
|
|
timeout=self.gettimeout(),
|
|
timeout_exc=socket.timeout)
|
|
tail += self.send(data[tail:], flags)
|
|
|
|
def sendto(self, *args):
|
|
trampoline(self.fd, write=True, timeout_exc=socket.timeout)
|
|
return self.fd.sendto(*args)
|
|
|
|
def setblocking(self, flag):
|
|
if flag:
|
|
self.act_non_blocking = False
|
|
self.timeout = None
|
|
else:
|
|
self.act_non_blocking = True
|
|
self.timeout = 0.0
|
|
|
|
def setsockopt(self, *args, **kw):
|
|
fn = self.setsockopt = self.fd.setsockopt
|
|
return fn(*args, **kw)
|
|
|
|
def shutdown(self, *args, **kw):
|
|
fn = self.shutdown = self.fd.shutdown
|
|
return fn(*args, **kw)
|
|
|
|
def settimeout(self, howlong):
|
|
if howlong is None or howlong == _GLOBAL_DEFAULT_TIMEOUT:
|
|
self.setblocking(True)
|
|
return
|
|
try:
|
|
f = howlong.__float__
|
|
except AttributeError:
|
|
raise TypeError('a float is required')
|
|
howlong = f()
|
|
if howlong < 0.0:
|
|
raise ValueError('Timeout value out of range')
|
|
if howlong == 0.0:
|
|
self.setblocking(howlong)
|
|
else:
|
|
self.timeout = howlong
|
|
|
|
def gettimeout(self):
|
|
return self.timeout
|
|
|
|
|
|
class GreenPipe(object):
|
|
newlines = '\r\n'
|
|
def __init__(self, fd):
|
|
set_nonblocking(fd)
|
|
self.fd = fd
|
|
self.closed = False
|
|
self.recvbuffer = ''
|
|
|
|
def close(self):
|
|
self.fd.close()
|
|
self.closed = True
|
|
|
|
def fileno(self):
|
|
return self.fd.fileno()
|
|
|
|
def read(self, buflen, flags=0):
|
|
fd = self.fd
|
|
if buflen is None:
|
|
buflen = BUFFER_SIZE
|
|
buf = self.recvbuffer
|
|
if buf:
|
|
chunk, self.recvbuffer = buf[:buflen], buf[buflen:]
|
|
return chunk
|
|
while True:
|
|
try:
|
|
return fd.read(buflen)
|
|
except IOError, e:
|
|
if e[0] != EAGAIN:
|
|
return ''
|
|
except socket.error, e:
|
|
if e[0] == errno.EPIPE:
|
|
return ''
|
|
raise
|
|
trampoline(fd, read=True)
|
|
|
|
def write(self, data, flags=0):
|
|
fd = self.fd
|
|
tail = 0
|
|
len_data = len(data)
|
|
while tail < len_data:
|
|
tosend = data[tail:]
|
|
try:
|
|
fd.write(tosend)
|
|
fd.flush()
|
|
tail += len(tosend)
|
|
if tail == len_data:
|
|
return len_data
|
|
except IOError, e:
|
|
if e[0] != EAGAIN:
|
|
raise
|
|
except ValueError, e:
|
|
pass
|
|
except socket.error, e:
|
|
if e[0] != errno.EPIPE:
|
|
raise
|
|
trampoline(fd, write=True)
|
|
|
|
def flush(self):
|
|
pass
|
|
|
|
def readuntil(self, terminator, size=None):
|
|
buf, self.recvbuffer = self.recvbuffer, ''
|
|
checked = 0
|
|
if size is None:
|
|
while True:
|
|
found = buf.find(terminator, checked)
|
|
if found != -1:
|
|
found += len(terminator)
|
|
chunk, self.recvbuffer = buf[:found], buf[found:]
|
|
return chunk
|
|
checked = max(0, len(buf) - (len(terminator) - 1))
|
|
d = self.fd.read(BUFFER_SIZE)
|
|
if not d:
|
|
break
|
|
buf += d
|
|
return buf
|
|
while len(buf) < size:
|
|
found = buf.find(terminator, checked)
|
|
if found != -1:
|
|
found += len(terminator)
|
|
chunk, self.recvbuffer = buf[:found], buf[found:]
|
|
return chunk
|
|
checked = len(buf)
|
|
d = self.fd.read(BUFFER_SIZE)
|
|
if not d:
|
|
break
|
|
buf += d
|
|
chunk, self.recvbuffer = buf[:size], buf[size:]
|
|
return chunk
|
|
|
|
def readline(self, size=None):
|
|
return self.readuntil(self.newlines, size=size)
|
|
|
|
def __iter__(self):
|
|
return self.xreadlines()
|
|
|
|
def xreadlines(self, size=None):
|
|
if size is None:
|
|
while True:
|
|
line = self.readline()
|
|
if not line:
|
|
break
|
|
yield line
|
|
else:
|
|
while size > 0:
|
|
line = self.readline(size)
|
|
if not line:
|
|
break
|
|
yield line
|
|
size -= len(line)
|
|
|
|
def writelines(self, lines):
|
|
for line in lines:
|
|
self.write(line)
|
|
|
|
|
|
# import SSL module here so we can refer to greenio.SSL.exceptionclass
|
|
try:
|
|
from OpenSSL import SSL
|
|
except ImportError:
|
|
# pyOpenSSL not installed, define exceptions anyway for convenience
|
|
class SSL(object):
|
|
class WantWriteError(object):
|
|
pass
|
|
|
|
class WantReadError(object):
|
|
pass
|
|
|
|
class ZeroReturnError(object):
|
|
pass
|
|
|
|
class SysCallError(object):
|
|
pass
|
|
|
|
|
|
def shutdown_safe(sock):
|
|
""" Shuts down the socket. This is a convenience method for
|
|
code that wants to gracefully handle regular sockets, SSL.Connection
|
|
sockets from PyOpenSSL and ssl.SSLSocket objects from Python 2.6
|
|
interchangeably. Both types of ssl socket require a shutdown() before
|
|
close, but they have different arity on their shutdown method.
|
|
|
|
Regular sockets don't need a shutdown before close, but it doesn't hurt.
|
|
"""
|
|
try:
|
|
try:
|
|
# socket, ssl.SSLSocket
|
|
return sock.shutdown(socket.SHUT_RDWR)
|
|
except TypeError:
|
|
# SSL.Connection
|
|
return sock.shutdown()
|
|
except socket.error, e:
|
|
# we don't care if the socket is already closed;
|
|
# this will often be the case in an http server context
|
|
if e[0] != errno.ENOTCONN:
|
|
raise
|
|
|