本文整理汇总了Python中gevent.socket.socket方法的典型用法代码示例。如果您正苦于以下问题:Python socket.socket方法的具体用法?Python socket.socket怎么用?Python socket.socket使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gevent.socket
的用法示例。
在下文中一共展示了socket.socket方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: patch
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import socket [as 别名]
def patch(self):
from gevent import monkey
monkey.noisy = False
# if the new version is used make sure to patch subprocess
if gevent.version_info[0] == 0:
monkey.patch_all()
else:
monkey.patch_all(subprocess=True)
# monkey patch sendfile to make it none blocking
patch_sendfile()
# patch sockets
sockets = []
for s in self.sockets:
if sys.version_info[0] == 3:
sockets.append(socket(s.FAMILY, _socket.SOCK_STREAM,
fileno=s.sock.fileno()))
else:
sockets.append(socket(s.FAMILY, _socket.SOCK_STREAM,
_sock=s))
self.sockets = sockets
示例2: connect
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import socket [as 别名]
def connect(self, server_addr):
self._new_socket()
logger.debug("Attempting connection to %s", str(server_addr))
try:
self._connect(server_addr)
except socket.error:
return False
self.server_addr = server_addr
self.recv_queue.queue.clear()
self._reader = gevent.spawn(self._reader_loop)
self._writer = gevent.spawn(self._writer_loop)
logger.debug("Connected.")
self.event_connected.set()
return True
示例3: disconnect
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import socket [as 别名]
def disconnect(self):
if not self.event_connected.is_set():
return
self.event_connected.clear()
self.server_addr = None
if self._reader:
self._reader.kill(block=False)
self._reader = None
if self._writer:
self._writer.kill(block=False)
self._writer = None
self._readbuf = b''
self.send_queue.queue.clear()
self.recv_queue.queue.clear()
self.recv_queue.put(StopIteration)
self.socket.close()
logger.debug("Disconnected.")
示例4: __init__
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import socket [as 别名]
def __init__(self,
zmq_context,
zmq_proxy_xsub_url=ait.SERVER_DEFAULT_XSUB_URL,
zmq_proxy_xpub_url=ait.SERVER_DEFAULT_XPUB_URL,
**kwargs):
if 'input' in kwargs and type(kwargs['input'][0]) is int:
super(PortInputClient, self).__init__(zmq_context,
zmq_proxy_xsub_url,
zmq_proxy_xpub_url,
listener=int(kwargs['input'][0]))
else:
raise(ValueError('Input must be port in order to create PortInputClient'))
# open sub socket
self.sub = gevent.socket.socket(gevent.socket.AF_INET, gevent.socket.SOCK_DGRAM)
示例5: patch
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import socket [as 别名]
def patch(self):
from gevent import monkey
monkey.noisy = False
# if the new version is used make sure to patch subprocess
if gevent.version_info[0] == 0:
monkey.patch_all()
else:
monkey.patch_all(subprocess=True)
# monkey patch sendfile to make it none blocking
patch_sendfile()
# patch sockets
sockets = []
for s in self.sockets:
sockets.append(socket(s.FAMILY, _socket.SOCK_STREAM,
_sock=s))
self.sockets = sockets
示例6: green_threads
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import socket [as 别名]
def green_threads(self, val):
if val and socket.socket is not gevent.socket.socket:
warn("""
WARNING: green_threads is set but this process is
not monkey patched. This will cause severely degraded
performance.
CloudVolume uses gevent for cooperative (green)
threading but it requires patching the Python standard
library to perform asynchronous IO. Add this code to
the top of your program (before any other imports):
import gevent.monkey
gevent.monkey.patch_all(threads=False)
More Information:
http://www.gevent.org/intro.html#monkey-patching
""")
self.config.green = bool(val)
示例7: __init__
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import socket [as 别名]
def __init__(self, listener, handle=None, backlog=None, spawn='default', **ssl_args):
BaseServer.__init__(self, listener, handle=handle, spawn=spawn)
try:
if ssl_args:
ssl_args.setdefault('server_side', True)
if 'ssl_context' in ssl_args:
ssl_context = ssl_args.pop('ssl_context')
self.wrap_socket = ssl_context.wrap_socket
self.ssl_args = ssl_args
else:
from gevent.ssl import wrap_socket
self.wrap_socket = wrap_socket
self.ssl_args = ssl_args
else:
self.ssl_args = None
if backlog is not None:
if hasattr(self, 'socket'):
raise TypeError('backlog must be None when a socket instance is passed')
self.backlog = backlog
except:
self.close()
raise
示例8: _udp_socket
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import socket [as 别名]
def _udp_socket(address, backlog=50, reuse_addr=None, family=_socket.AF_INET):
# backlog argument for compat with tcp_listener
# pylint:disable=unused-argument
# we want gevent.socket.socket here
sock = socket(family=family, type=_socket.SOCK_DGRAM)
if reuse_addr is not None:
sock.setsockopt(_socket.SOL_SOCKET, _socket.SO_REUSEADDR, reuse_addr)
try:
sock.bind(address)
except _socket.error as ex:
strerror = getattr(ex, 'strerror', None)
if strerror is not None:
ex.strerror = strerror + ': ' + repr(address)
raise
return sock
示例9: run
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import socket [as 别名]
def run(self):
while True:
for socksProxy in self.config['socksProxies']:
s = socket.socket()
s.settimeout(self.config['checkTimeout'])
try:
socksProxy.connect(s, ('www.google.com', 80))
s.send(bytearray(b'GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n'))
data = s.recv(65536)
if data == b'' or data == '' or data == None:
raise Exception()
socksProxy.fail = False
self.config['logger'].debug('Proxy %s is alive.', (socksProxy.host, socksProxy.port))
except Exception:
socksProxy.failCount += 1
socksProxy.fail = True
self.config['logger'].debug('Proxy %s is dead.', (socksProxy.host, socksProxy.port))
finally:
s.close()
time.sleep(self.config['checkInterval'])
示例10: cleanup
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import socket [as 别名]
def cleanup(self):
super(TCPConnection, self).cleanup()
self.write_buffer = []
self.read_buffer = ''
if self.socket:
self.socket.close()
self.socket = None
if self.net_read:
self.net_read.kill()
self.net_read = None
if self.net_write:
self.net_write.kill()
self.net_write = None
if not self.connected:
return
self.client.handle_disconnected(self.user_abort)
示例11: is_port_open
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import socket [as 别名]
def is_port_open(host, port):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(3.0)
if s.connect_ex((host, int(port))) == 0:
return True
else:
return False
except Exception as e:
return False
finally:
try:
s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0))
s.close()
except Exception as e:
pass
示例12: run
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import socket [as 别名]
def run(self):
gevent.getcurrent().name = self.task_name
while True:
msg = self.outgoing_queue.get()
if not isinstance(msg, PeerDisconnectedMessage):
try:
msg_bytes = self.encode(msg)
self.send(msg_bytes)
except (ConnectionResetError, ConnectionAbortedError):
# Ignore a closed connection here. The reader will notice
# it and send us the DisconnectedMessage to tell us that
# we can close the socket and terminate
pass
else:
self.sock.close()
if msg.exception:
raise msg.exception
else:
break
self.logger.info('%s(%s): writer exiting gracefully' % (self.task_name, self.task_id))
示例13: client
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import socket [as 别名]
def client():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', PORT))
f = sock.makefile(mode='wr')
f.write(MSG)
f.flush()
assert f.readline() == MSG
f.close()
示例14: get_environ
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import socket [as 别名]
def get_environ(self):
env = super(PyWSGIHandler, self).get_environ()
env['gunicorn.sock'] = self.socket
env['RAW_URI'] = self.path
return env
示例15: start
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import socket [as 别名]
def start(self):
server = socket.socket()
server.bind((self.config['proxyHost'], self.config['proxyPort']))
server.listen(50)
certStore = CertificationStore(self.config)
while True:
try:
client, addr = server.accept()
Tunnel(self.config, client, certStore).start()
except Exception:
logging.exception('Exception in Main.start:')