本文整理汇总了Python中gevent.socket.create_connection方法的典型用法代码示例。如果您正苦于以下问题:Python socket.create_connection方法的具体用法?Python socket.create_connection怎么用?Python socket.create_connection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gevent.socket
的用法示例。
在下文中一共展示了socket.create_connection方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: connect
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import create_connection [as 别名]
def connect(self, address, remote_pubkey):
log.debug('connecting', address=address)
"""
gevent.socket.create_connection(address, timeout=Timeout, source_address=None)
Connect to address (a 2-tuple (host, port)) and return the socket object.
Passing the optional timeout parameter will set the timeout
getdefaulttimeout() is default
"""
try:
connection = create_connection(address, timeout=self.connect_timeout)
except socket.timeout:
log.info('connection timeout', address=address, timeout=self.connect_timeout)
self.errors.add(address, 'connection timeout')
return False
except socket.error as e:
log.info('connection error', errno=e.errno, reason=e.strerror)
self.errors.add(address, 'connection error')
return False
self._start_peer(connection, address, remote_pubkey)
return True
示例2: send_message_to_slave
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import create_connection [as 别名]
def send_message_to_slave(message, address):
try:
payload = generate_msgpack_message_payload(message)
except TypeError:
logger.exception('Failed encoding message %s as msgpack', message)
metrics.incr('rpc_message_pass_fail_cnt')
return False
pretty_address = '%s:%s' % address
message_id = message.get('message_id', '?')
try:
s = socket.create_connection(address)
s.send(payload)
sender_resp = msgpack_unpack_msg_from_socket(s)
s.close()
except socket.error:
logging.exception('Failed connecting to %s to send message (ID %s)',
pretty_address, message_id)
metrics.incr('rpc_message_pass_fail_cnt')
return False
if sender_resp == 'OK':
access_logger.info('Successfully passed message (ID %s) to %s for sending',
message_id, pretty_address)
metrics.incr('rpc_message_pass_success_cnt')
return True
else:
logger.error('Failed sending message (ID %s) through %s: %s',
message_id, pretty_address, sender_resp)
metrics.incr('rpc_message_pass_fail_cnt')
return False
示例3: connect_socks
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import create_connection [as 别名]
def connect_socks(self, host, port):
socksconn = None
try:
socksconn = socket.create_connection((self.socksip, self.socksport), timeout=self.timeout)
if not socksutils.basic_handshake_client(socksconn):
socksconn.close()
addrtype = socksutils.addr_type(host)
socksutils.send_request(socksconn, cmd=socksmsg.CONNECT,
addrtype=addrtype, dstaddr=host, dstport=port)
reply = socksutils.read_reply(socksconn)
if reply.rep != socksmsg.SUCCEEDED:
log.info("error response %d returned from socks server" % reply.rep)
socksconn.close()
return None
return socksconn
except Exception, e:
log.error("[Exception][connect_socks]: %s" % str(e))
if socksconn:
socksconn.close()
return None
示例4: get_server_certificate
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import create_connection [as 别名]
def get_server_certificate(addr, ssl_version=PROTOCOL_SSLv23, ca_certs=None):
"""Retrieve the certificate from the server at the specified address,
and return it as a PEM-encoded string.
If 'ca_certs' is specified, validate the server cert against it.
If 'ssl_version' is specified, use it in the connection attempt."""
_, _ = addr
if ca_certs is not None:
cert_reqs = CERT_REQUIRED
else:
cert_reqs = CERT_NONE
context = _create_stdlib_context(ssl_version,
cert_reqs=cert_reqs,
cafile=ca_certs)
with closing(create_connection(addr)) as sock:
with closing(context.wrap_socket(sock)) as sslsock:
dercert = sslsock.getpeercert(True)
return DER_cert_to_PEM_cert(dercert)
示例5: _create_socket
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import create_connection [as 别名]
def _create_socket(self):
ssl_context = ssl.SSLContext()
sock = socket.create_connection((self.host, self.port))
return ssl_context.wrap_socket(sock, server_hostname=self.host)
示例6: connect
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import create_connection [as 别名]
def connect(self):
if self.is_connected:
return
try:
self.socket = socket.create_connection(
address=(self.address, self.port),
timeout=self.timeout,
)
except socket.error as error:
six.raise_from(NSQSocketError(*error.args), error)
示例7: _create_socket
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import create_connection [as 别名]
def _create_socket(self):
return socket.create_connection((self.host, self.port))
示例8: handle
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import create_connection [as 别名]
def handle(self, source, address): # pylint:disable=method-hidden
log('%s:%s accepted', *address[:2])
try:
dest = create_connection(self.dest)
except IOError as ex:
log('%s:%s failed to connect to %s:%s: %s', address[
0], address[1], self.dest[0], self.dest[1], ex)
return
forwarders = (gevent.spawn(forward, source, dest, self),
gevent.spawn(forward, dest, source, self))
gevent.joinall(forwarders)