本文整理汇总了Python中select.select方法的典型用法代码示例。如果您正苦于以下问题:Python select.select方法的具体用法?Python select.select怎么用?Python select.select使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类select
的用法示例。
在下文中一共展示了select.select方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: import select [as 别名]
# 或者: from select import select [as 别名]
def run(self):
while not self._cancel:
if self._client is None:
endpoints = [self._server]
else:
endpoints = [self._server, self._client]
read, _write, error = select.select(endpoints, [], [], 0.5)
for sock in read:
if sock is self._server:
try:
client, _addr = self._server.accept()
if self._client is not None:
self._client.close()
self._client = client
except socket.error:
self._client = None
for sock in error:
if sock is self._client:
self._client = None
sock.close()
示例2: _doSSLHandshake
# 需要导入模块: import select [as 别名]
# 或者: from select import select [as 别名]
def _doSSLHandshake(self) :
count = 0
while count < 10 :
try :
self._socket.do_handshake()
break
except ssl.SSLError as sslErr :
count += 1
if sslErr.args[0] == ssl.SSL_ERROR_WANT_READ :
select([self._socket], [], [], 1)
elif sslErr.args[0] == ssl.SSL_ERROR_WANT_WRITE :
select([], [self._socket], [], 1)
else :
raise XAsyncTCPClientException('SSL : Bad handshake : %s' % sslErr)
except Exception as ex :
raise XAsyncTCPClientException('SSL : Handshake error : %s' % ex)
# ------------------------------------------------------------------------
示例3: listen
# 需要导入模块: import select [as 别名]
# 或者: from select import select [as 别名]
def listen(server, on_read):
sock, addr = server.accept()
timeout = 10
try:
read_ready, _, _ = select.select([sock], [], [], timeout)
while len(read_ready):
data = sock.recv(8192)
if on_read(sock, data):
read_ready, _, _ = select.select([sock], [], [], 0)
else:
read_ready = []
except (socket.error, select.error, OSError, ValueError):
pass
try:
sock.shutdown(socket.SHUT_RDWR)
except (socket.error, OSError, ValueError):
pass
sock.close()
示例4: proxy
# 需要导入模块: import select [as 别名]
# 或者: from select import select [as 别名]
def proxy(src, dst, callback=None):
timeout = 10
try:
read_ready, _, _ = select.select([src], [], [], timeout)
while len(read_ready):
if callback:
callback(src, dst)
else:
dst.send(src.recv(8192))
read_ready, _, _ = select.select([src], [], [], timeout)
except (socket.error, select.error, OSError, ValueError):
pass
try:
src.shutdown(socket.SHUT_RDWR)
except (socket.error, OSError, ValueError):
pass
src.close()
try:
dst.shutdown(socket.SHUT_RDWR)
except (socket.error, OSError, ValueError):
pass
dst.close()
示例5: select_read
# 需要导入模块: import select [as 别名]
# 或者: from select import select [as 别名]
def select_read(self, timeout=None):
"""
Blocks until the socket is ready to be read from, or the timeout is hit
:param timeout:
A float - the period of time to wait for data to be read. None for
no time limit.
:return:
A boolean - if data is ready to be read. Will only be False if
timeout is not None.
"""
# If we have buffered data, we consider a read possible
if len(self._decrypted_bytes) > 0:
return True
read_ready, _, _ = select.select([self._socket], [], [], timeout)
return len(read_ready) > 0
示例6: recv
# 需要导入模块: import select [as 别名]
# 或者: from select import select [as 别名]
def recv(self, *args, **kwargs):
try:
data = self.connection.recv(*args, **kwargs)
except OpenSSL.SSL.SysCallError as e:
if self.suppress_ragged_eofs and e.args == (-1, 'Unexpected EOF'):
return b''
else:
raise
except OpenSSL.SSL.ZeroReturnError as e:
if self.connection.get_shutdown() == OpenSSL.SSL.RECEIVED_SHUTDOWN:
return b''
else:
raise
except OpenSSL.SSL.WantReadError:
rd, wd, ed = select.select(
[self.socket], [], [], self.socket.gettimeout())
if not rd:
raise timeout('The read operation timed out')
else:
return self.recv(*args, **kwargs)
else:
return data
示例7: interact
# 需要导入模块: import select [as 别名]
# 或者: from select import select [as 别名]
def interact(self):
"""
Interact with the socket. This will send all keyboard input to the
socket and input from the socket to the console until an EOF occurs.
"""
sockets = [sys.stdin, self.channel]
while True:
ready = select.select(sockets, [], [])[0]
if sys.stdin in ready:
line = sys.stdin.readline().encode('latin1')
if not line:
break
self.write(line)
if self.channel in ready:
self.read(1, echo=True)
示例8: _retry_on_intr
# 需要导入模块: import select [as 别名]
# 或者: from select import select [as 别名]
def _retry_on_intr(fn, timeout):
if timeout is None:
deadline = float("inf")
else:
deadline = monotonic() + timeout
while True:
try:
return fn(timeout)
# OSError for 3 <= pyver < 3.5, select.error for pyver <= 2.7
except (OSError, select.error) as e:
# 'e.args[0]' incantation works for both OSError and select.error
if e.args[0] != errno.EINTR:
raise
else:
timeout = deadline - monotonic()
if timeout < 0:
timeout = 0
if timeout == float("inf"):
timeout = None
continue
示例9: select_wait_for_socket
# 需要导入模块: import select [as 别名]
# 或者: from select import select [as 别名]
def select_wait_for_socket(sock, read=False, write=False, timeout=None):
if not read and not write:
raise RuntimeError("must specify at least one of read=True, write=True")
rcheck = []
wcheck = []
if read:
rcheck.append(sock)
if write:
wcheck.append(sock)
# When doing a non-blocking connect, most systems signal success by
# marking the socket writable. Windows, though, signals success by marked
# it as "exceptional". We paper over the difference by checking the write
# sockets for both conditions. (The stdlib selectors module does the same
# thing.)
fn = partial(select.select, rcheck, wcheck, wcheck)
rready, wready, xready = _retry_on_intr(fn, timeout)
return bool(rready or wready or xready)
示例10: poll_wait_for_socket
# 需要导入模块: import select [as 别名]
# 或者: from select import select [as 别名]
def poll_wait_for_socket(sock, read=False, write=False, timeout=None):
if not read and not write:
raise RuntimeError("must specify at least one of read=True, write=True")
mask = 0
if read:
mask |= select.POLLIN
if write:
mask |= select.POLLOUT
poll_obj = select.poll()
poll_obj.register(sock, mask)
# For some reason, poll() takes timeout in milliseconds
def do_poll(t):
if t is not None:
t *= 1000
return poll_obj.poll(t)
return bool(_retry_on_intr(do_poll, timeout))
示例11: ask_password
# 需要导入模块: import select [as 别名]
# 或者: from select import select [as 别名]
def ask_password(profile, interactive):
"""
Prompt for profile password
"""
if not PY3:
profile = profile.encode(SYS_ENCODING)
passmsg = "\nMaster Password for profile {0}: ".format(profile)
if sys.stdin.isatty() and interactive:
passwd = getpass(passmsg)
else:
# Ability to read the password from stdin (echo "pass" | ./firefox_...)
if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
passwd = sys.stdin.readline().rstrip("\n")
else:
LOG.warning("Master Password not provided, continuing with blank password")
passwd = ""
return py2_decode(passwd)
示例12: serve_forever
# 需要导入模块: import select [as 别名]
# 或者: from select import select [as 别名]
def serve_forever(self, poll_interval=0.5):
"""Handle one request at a time until shutdown.
Polls for shutdown every poll_interval seconds. Ignores
self.timeout. If you need to do periodic tasks, do them in
another thread.
"""
self.__is_shut_down.clear()
try:
while not self.__shutdown_request:
# XXX: Consider using another file descriptor or
# connecting to the socket to wake this up instead of
# polling. Polling reduces our responsiveness to a
# shutdown request and wastes cpu at all other times.
r, w, e = _eintr_retry(select.select, [self], [], [],
poll_interval)
if self in r:
self._handle_request_noblock()
self.service_actions()
finally:
self.__shutdown_request = False
self.__is_shut_down.set()
示例13: service_actions
# 需要导入模块: import select [as 别名]
# 或者: from select import select [as 别名]
def service_actions(self):
"""Called by the serve_forever() loop.
May be overridden by a subclass / Mixin to implement any code that
needs to be run during the loop.
"""
pass
# The distinction between handling, getting, processing and
# finishing a request is fairly arbitrary. Remember:
#
# - handle_request() is the top-level call. It calls
# select, get_request(), verify_request() and process_request()
# - get_request() is different for stream or datagram sockets
# - process_request() is the place that may fork a new process
# or create a new thread to finish the request
# - finish_request() instantiates the request handler class;
# this constructor will handle the request all by itself
示例14: handle_request
# 需要导入模块: import select [as 别名]
# 或者: from select import select [as 别名]
def handle_request(self):
"""Handle one request, possibly blocking.
Respects self.timeout.
"""
# Support people who used socket.settimeout() to escape
# handle_request before self.timeout was available.
timeout = self.socket.gettimeout()
if timeout is None:
timeout = self.timeout
elif self.timeout is not None:
timeout = min(timeout, self.timeout)
fd_sets = _eintr_retry(select.select, [self], [], [], timeout)
if not fd_sets[0]:
self.handle_timeout()
return
self._handle_request_noblock()
示例15: _handle_request_noblock
# 需要导入模块: import select [as 别名]
# 或者: from select import select [as 别名]
def _handle_request_noblock(self):
"""Handle one request, without blocking.
I assume that select.select has returned that the socket is
readable before this function was called, so there should be
no risk of blocking in get_request().
"""
try:
request, client_address = self.get_request()
except socket.error:
return
if self.verify_request(request, client_address):
try:
self.process_request(request, client_address)
except:
self.handle_error(request, client_address)
self.shutdown_request(request)