当前位置: 首页>>代码示例>>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;未经允许,请勿转载。