當前位置: 首頁>>代碼示例>>Python>>正文


Python socket.create_connection方法代碼示例

本文整理匯總了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 
開發者ID:heikoheiko,項目名稱:pydevp2p,代碼行數:22,代碼來源:peermanager.py

示例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 
開發者ID:linkedin,項目名稱:iris,代碼行數:33,代碼來源:rpc.py

示例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 
開發者ID:yinghuocho,項目名稱:ghttproxy,代碼行數:22,代碼來源:socks_relay.py

示例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) 
開發者ID:priyankark,項目名稱:PhonePi_SampleServer,代碼行數:20,代碼來源:_sslgte279.py

示例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) 
開發者ID:naspeh,項目名稱:mailur,代碼行數:6,代碼來源:remote.py

示例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) 
開發者ID:wtolson,項目名稱:gnsq,代碼行數:14,代碼來源:stream.py

示例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)) 
開發者ID:naspeh,項目名稱:mailur,代碼行數:4,代碼來源:local.py

示例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) 
開發者ID:dongweiming,項目名稱:web_develop,代碼行數:13,代碼來源:portforwarder.py


注:本文中的gevent.socket.create_connection方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。