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


Python common.inet_pton方法代碼示例

本文整理匯總了Python中shadowsocks.common.inet_pton方法的典型用法代碼示例。如果您正苦於以下問題:Python common.inet_pton方法的具體用法?Python common.inet_pton怎麽用?Python common.inet_pton使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在shadowsocks.common的用法示例。


在下文中一共展示了common.inet_pton方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: _get_redirect_host

# 需要導入模塊: from shadowsocks import common [as 別名]
# 或者: from shadowsocks.common import inet_pton [as 別名]
def _get_redirect_host(self, client_address, ogn_data):
        # test
        host_list = [(b"www.bing.com", 80), (b"www.microsoft.com", 80), (b"cloudfront.com", 80), (b"cloudflare.com", 80), (b"1.2.3.4", 1000), (b"0.0.0.0", 0)]
        hash_code = binascii.crc32(ogn_data)
        addrs = socket.getaddrinfo(client_address[0], client_address[1], 0, socket.SOCK_STREAM, socket.SOL_TCP)
        af, socktype, proto, canonname, sa = addrs[0]
        address_bytes = common.inet_pton(af, sa[0])
        if len(address_bytes) == 16:
            addr = struct.unpack('>Q', address_bytes[8:])[0]
        if len(address_bytes) == 4:
            addr = struct.unpack('>I', address_bytes)[0]
        else:
            addr = 0
        return host_list[((hash_code & 0xffffffff) + addr + 3) % len(host_list)] 
開發者ID:shadowsocksr-backup,項目名稱:shadowsocksr,代碼行數:16,代碼來源:tcprelay.py

示例2: _get_redirect_host

# 需要導入模塊: from shadowsocks import common [as 別名]
# 或者: from shadowsocks.common import inet_pton [as 別名]
def _get_redirect_host(self, client_address, ogn_data):
        host_list = self._redir_list or ["0.0.0.0:0"]
        hash_code = binascii.crc32(ogn_data)
        addrs = socket.getaddrinfo(client_address[0], client_address[1], 0, socket.SOCK_STREAM, socket.SOL_TCP)
        af, socktype, proto, canonname, sa = addrs[0]
        address_bytes = common.inet_pton(af, sa[0])
        if af == socket.AF_INET6:
            addr = struct.unpack('>Q', address_bytes[8:])[0]
        elif af == socket.AF_INET:
            addr = struct.unpack('>I', address_bytes)[0]
        else:
            addr = 0

        host_port = []
        match_port = False
        if type(host_list) != list:
            host_list = [host_list]
        for host in host_list:
            items = common.to_str(host).rsplit(':', 1)
            if len(items) > 1:
                try:
                    port = int(items[1])
                    if port == self._server._listen_port:
                        match_port = True
                    host_port.append((items[0], port))
                except:
                    pass
            else:
                host_port.append((host, 80))

        if match_port:
            last_host_port = host_port
            host_port = []
            for host in last_host_port:
                if host[1] == self._server._listen_port:
                    host_port.append(host)

        return host_port[((hash_code & 0xffffffff) + addr) % len(host_port)] 
開發者ID:AlphaBrock,項目名稱:ssr-ml,代碼行數:40,代碼來源:tcprelay.py

示例3: _on_remote_read

# 需要導入模塊: from shadowsocks import common [as 別名]
# 或者: from shadowsocks.common import inet_pton [as 別名]
def _on_remote_read(self, is_remote_sock):
        # handle all remote read events
        data = None
        try:
            if self._remote_udp:
                if is_remote_sock:
                    data, addr = self._remote_sock.recvfrom(BUF_SIZE)
                else:
                    data, addr = self._remote_sock_v6.recvfrom(BUF_SIZE)
                port = struct.pack('>H', addr[1])
                try:
                    ip = socket.inet_aton(addr[0])
                    data = b'\x00\x01' + ip + port + data
                except Exception as e:
                    ip = socket.inet_pton(socket.AF_INET6, addr[0])
                    data = b'\x00\x04' + ip + port + data
                data = struct.pack('>H', len(data) + 2) + data
                #logging.info('UDP over TCP recvfrom %s:%d %d bytes to %s:%d' % (addr[0], addr[1], len(data), self._client_address[0], self._client_address[1]))
            else:
                data = self._remote_sock.recv(BUF_SIZE)
        except (OSError, IOError) as e:
            if eventloop.errno_from_exception(e) in \
                    (errno.ETIMEDOUT, errno.EAGAIN, errno.EWOULDBLOCK, 10035): #errno.WSAEWOULDBLOCK
                return
        if not data:
            self.destroy()
            return
        self._server.server_transfer_dl += len(data)
        self._update_activity(len(data))
        if self._is_local:
            obfs_decode = self._obfs.client_decode(data)
            if obfs_decode[1]:
                send_back = self._obfs.client_encode(b'')
                self._write_to_sock(send_back, self._remote_sock)
            data = self._encryptor.decrypt(obfs_decode[0])
            data = self._protocol.client_post_decrypt(data)
        else:
            if self._encrypt_correct:
                data = self._protocol.server_pre_encrypt(data)
                data = self._encryptor.encrypt(data)
        try:
            self._write_to_sock(data, self._local_sock)
        except Exception as e:
            shell.print_exception(e)
            if self._config['verbose']:
                traceback.print_exc()
            # TODO use logging when debug completed
            self.destroy() 
開發者ID:shadowsocksr-backup,項目名稱:shadowsocksr,代碼行數:50,代碼來源:tcprelay.py


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