当前位置: 首页>>代码示例>>Python>>正文


Python netutil.is_valid_ip函数代码示例

本文整理汇总了Python中tornado.netutil.is_valid_ip函数的典型用法代码示例。如果您正苦于以下问题:Python is_valid_ip函数的具体用法?Python is_valid_ip怎么用?Python is_valid_ip使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了is_valid_ip函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: resolve

 def resolve(self, host, port, family=0, callback=None):
     if is_valid_ip(host):
         addresses = [host]
     else:
         # gethostbyname doesn't take callback as a kwarg
         self.channel.gethostbyname(host, family, (yield gen.Callback(1)))
         callback_args = yield gen.Wait(1)
         assert isinstance(callback_args, gen.Arguments)
         assert not callback_args.kwargs
         result, error = callback_args.args
         if error:
             raise Exception('C-Ares returned error %s: %s while resolving %s' %
                             (error, pycares.errno.strerror(error), host))
         addresses = result.addresses
     addrinfo = []
     for address in addresses:
         if '.' in address:
             address_family = socket.AF_INET
         elif ':' in address:
             address_family = socket.AF_INET6
         else:
             address_family = socket.AF_UNSPEC
         if family != socket.AF_UNSPEC and family != address_family:
             raise Exception('Requested socket family %d but got %d' %
                             (family, address_family))
         addrinfo.append((address_family, (address, port)))
     callback(addrinfo)
开发者ID:Caetano,项目名称:tornado,代码行数:27,代码来源:caresresolver.py

示例2: resolve

 def resolve(self, host, port, family=0):
     if is_valid_ip(host):
         addresses = [host]
     else:
         # gethostbyname doesn't take callback as a kwarg
         fut = Future()
         self.channel.gethostbyname(host, family,
                                    lambda result, error: fut.set_result((result, error)))
         result, error = yield fut
         if error:
             raise IOError('C-Ares returned error %s: %s while resolving %s' %
                           (error, pycares.errno.strerror(error), host))
         addresses = result.addresses
     addrinfo = []
     for address in addresses:
         if '.' in address:
             address_family = socket.AF_INET
         elif ':' in address:
             address_family = socket.AF_INET6
         else:
             address_family = socket.AF_UNSPEC
         if family != socket.AF_UNSPEC and family != address_family:
             raise IOError('Requested socket family %d but got %d' %
                           (family, address_family))
         addrinfo.append((address_family, (address, port)))
     raise gen.Return(addrinfo)
开发者ID:Agnewee,项目名称:tornado,代码行数:26,代码来源:caresresolver.py

示例3: __init__

    def __init__(self, method, uri, version="HTTP/1.0", headers=None,
                 body=None, remote_ip=None, protocol=None, host=None,
                 files=None, connection=None):
        self.method = method
        self.uri = uri
        self.version = version
        self.headers = headers or httputil.HTTPHeaders()
        self.body = body or ""
        self.trace = None

        # set remote IP and protocol
        self.remote_ip = remote_ip
        if protocol:
            self.protocol = protocol
        elif connection and isinstance(connection.stream,
                                       iostream.SSLIOStream):
            self.protocol = "https"
        else:
            self.protocol = "http"

        # xheaders can override the defaults
        if connection and connection.xheaders:
            # Squid uses X-Forwarded-For, others use X-Real-Ip
            ip = self.headers.get("X-Forwarded-For", self.remote_ip)
            ip = ip.split(',')[-1].strip()
            ip = self.headers.get(
                "X-Real-Ip", ip)
            if netutil.is_valid_ip(ip):
                self.remote_ip = ip
            # AWS uses X-Forwarded-Proto
            proto = self.headers.get(
                "X-Scheme", self.headers.get("X-Forwarded-Proto", self.protocol))
            if proto in ("http", "https"):
                self.protocol = proto
            # Zipkin users 
            if options.server_trace:
                parent_span_id = self.headers.get("X-B3-ParentSpanId", None)
                trace_id = self.headers.get("X-B3-TraceId", None)
                span_id = self.headers.get("X-B3-SpanId", None)
                name =  method
                endpoint = Endpoint(ipv4=socket.gethostbyname(socket.gethostname()), port=port, service_name=service_name)
                
                self.trace = Trace(name=name, trace_id=trace_id, span_id=span_id, parent_span_id=parent_span_id)
                self.trace.set_endpoint(endpoint)

        self.host = host or self.headers.get("Host") or "127.0.0.1"
        self.files = files or {}
        self.connection = connection
        self._start_time = time.time()
        self._finish_time = None

        self.path, sep, self.query = uri.partition('?')
        self.arguments = parse_qs_bytes(self.query, keep_blank_values=True)
        self.query_arguments = copy.deepcopy(self.arguments)
        self.body_arguments = {}
        
        if options.server_trace:
            self.trace.record(Annotation.string('Url', uri))
            self.trace.record(Annotation.string('Header', self.headers))
            self.trace.record(Annotation.server_recv())
开发者ID:iambocai,项目名称:tornado,代码行数:60,代码来源:httpserver.py

示例4: _apply_xheaders

 def _apply_xheaders(self, headers):
     """Rewrite the ``remote_ip`` and ``protocol`` fields."""
     # 1. X-Forwarded-For: 记录一个请求从客户端出发到目标服务器过程中经历的代理或
     # 者负载平衡设备的IP。最早由 Squid 的开发人员引入,现已成为事实上的标准被广泛使用。
     # 一般格式为: client1, proxy1, proxy2,其中的值通过一个 “逗号+空格” 把多
     # 个 IP 地址分开,最左边(client1)是最原始客户端的IP地址, 代理服务器每成功收到
     # 一个请求,就把请求来源 IP 地址添加到右边(所以最后一个代理的 IP 不在该字段中,但
     # 可以通过 socket.getpeername() 获取,这个 Ip 是可靠的,伪造 Ip 的是无法通过
     #  TCP 三次握手的)。
     #
     # 2. X-Real-Ip:不是标准的 Http 头,不过 Nginx 在使用。通常被 HTTP 代理用来
     # 表示与它产生 TCP 连接的设备 IP,这个设备可能是其他代理,也可能是真正的请求端。
     #
     # 3. 经过 Nginx 的 Http 请求,X-Forwarded-For 的最后一个 Ip 与 X-Real-Ip
     # 是相同的,记录的是直接与 Nginx 建立连接的端 Ip。
     #
     # 4. 安全问题:伪造 X-Forwarded-For 字段很容易,所以使用该字段信息时要格外注意。
     # 通常 X-Forwarded-For 是最后一个IP地址是最后一个代理服务器的IP地址,这通常是一
     # 个比较可靠的信息来源。
     #
     # Squid uses X-Forwarded-For, others use X-Real-Ip
     ip = headers.get("X-Forwarded-For", self.remote_ip)
     ip = ip.split(",")[-1].strip()
     ip = headers.get("X-Real-Ip", ip)
     if netutil.is_valid_ip(ip):
         self.remote_ip = ip
     # AWS uses X-Forwarded-Proto
     proto_header = headers.get("X-Scheme", headers.get("X-Forwarded-Proto", self.protocol))
     if proto_header in ("http", "https"):
         self.protocol = proto_header
开发者ID:Strawhatfy,项目名称:tornado_4.0.1.annotation,代码行数:30,代码来源:httpserver.py

示例5: test_is_valid_ip

 def test_is_valid_ip(self):
     self.assertTrue(is_valid_ip('127.0.0.1'))
     self.assertTrue(is_valid_ip('4.4.4.4'))
     self.assertTrue(is_valid_ip('::1'))
     self.assertTrue(is_valid_ip('2620:0:1cfe:face:b00c::3'))
     self.assertTrue(not is_valid_ip('www.google.com'))
     self.assertTrue(not is_valid_ip('localhost'))
     self.assertTrue(not is_valid_ip('4.4.4.4<'))
     self.assertTrue(not is_valid_ip(' 127.0.0.1'))
开发者ID:jiangsong,项目名称:tornado_pubsub,代码行数:9,代码来源:netutil_test.py

示例6: _apply_xheaders

 def _apply_xheaders(self, headers):
     """Rewrite the ``remote_ip`` and ``protocol`` fields."""
     # Squid uses X-Forwarded-For, others use X-Real-Ip
     ip = headers.get("X-Forwarded-For", self.remote_ip)
     ip = ip.split(",")[-1].strip()
     ip = headers.get("X-Real-Ip", ip)
     if netutil.is_valid_ip(ip):
         self.remote_ip = ip
     # AWS uses X-Forwarded-Proto
     proto_header = headers.get("X-Scheme", headers.get("X-Forwarded-Proto", self.protocol))
     if proto_header in ("http", "https"):
         self.protocol = proto_header
开发者ID:spengx,项目名称:tornado,代码行数:12,代码来源:httpserver.py

示例7: __init__

    def __init__(
        self,
        method,
        uri,
        version="HTTP/1.0",
        headers=None,
        body=None,
        remote_ip=None,
        protocol=None,
        host=None,
        files=None,
        connection=None,
    ):
        self.method = method
        self.uri = uri
        self.version = version
        self.headers = headers or httputil.HTTPHeaders()
        self.body = body or ""

        # set remote IP and protocol
        self.remote_ip = remote_ip
        if protocol:
            self.protocol = protocol
        elif connection and isinstance(connection.stream, iostream.SSLIOStream):
            self.protocol = "https"
        else:
            self.protocol = "http"

        # xheaders can override the defaults
        if connection and connection.xheaders:
            # Squid uses X-Forwarded-For, others use X-Real-Ip
            ip = self.headers.get("X-Forwarded-For", self.remote_ip)
            ip = ip.split(",")[-1].strip()
            ip = self.headers.get("X-Real-Ip", ip)
            if netutil.is_valid_ip(ip):
                self.remote_ip = ip
            # AWS uses X-Forwarded-Proto
            proto = self.headers.get("X-Scheme", self.headers.get("X-Forwarded-Proto", self.protocol))
            if proto in ("http", "https"):
                self.protocol = proto

        self.host = host or self.headers.get("Host") or "127.0.0.1"
        self.files = files or {}
        self.connection = connection
        self._start_time = time.time()
        self._finish_time = None

        self.path, sep, self.query = uri.partition("?")
        self.arguments = parse_qs_bytes(self.query, keep_blank_values=True)
        self.query_arguments = copy.deepcopy(self.arguments)
        self.body_arguments = {}
开发者ID:naeemattari7,项目名称:tornado,代码行数:51,代码来源:httpserver.py

示例8: _apply_xheaders

 def _apply_xheaders(self, headers):
     """Rewrite the ``remote_ip`` and ``protocol`` fields."""
     # Squid uses X-Forwarded-For, others use X-Real-Ip
     ip = headers.get("X-Forwarded-For", self.remote_ip)
     # Skip trusted downstream hosts in X-Forwarded-For list
     for ip in (cand.strip() for cand in reversed(ip.split(','))):
         if ip not in self.trusted_downstream:
             break
     ip = headers.get("X-Real-Ip", ip)
     if netutil.is_valid_ip(ip):
         self.remote_ip = ip
     # AWS uses X-Forwarded-Proto
     proto_header = headers.get(
         "X-Scheme", headers.get("X-Forwarded-Proto",
                                 self.protocol))
     if proto_header in ("http", "https"):
         self.protocol = proto_header
开发者ID:ArthurGarnier,项目名称:SickRage,代码行数:17,代码来源:httpserver.py

示例9: _apply_xheaders

 def _apply_xheaders(self, headers):
     """Rewrite the ``remote_ip`` and ``protocol`` fields."""
     # Squid uses X-Forwarded-For, others use X-Real-Ip
     ip = headers.get("X-Forwarded-For", self.remote_ip)
     # Skip trusted downstream hosts in X-Forwarded-For list
     for ip in (cand.strip() for cand in reversed(ip.split(','))):
         if ip not in self.trusted_downstream:
             break
     ip = headers.get("X-Real-Ip", ip)
     if netutil.is_valid_ip(ip):
         self.remote_ip = ip
     # AWS uses X-Forwarded-Proto
     proto_header = headers.get(
         "X-Scheme", headers.get("X-Forwarded-Proto",
                                 self.protocol))
     if proto_header:
         # use only the last proto entry if there is more than one
         # TODO: support trusting mutiple layers of proxied protocol
         proto_header = proto_header.split(',')[-1].strip()
     if proto_header in ("http", "https"):
         self.protocol = proto_header
开发者ID:leeclemens,项目名称:tornado,代码行数:21,代码来源:httpserver.py

示例10: test_is_valid_ip

 def test_is_valid_ip(self):
     self.assertTrue(is_valid_ip("127.0.0.1"))
     self.assertTrue(is_valid_ip("4.4.4.4"))
     self.assertTrue(is_valid_ip("::1"))
     self.assertTrue(is_valid_ip("2620:0:1cfe:face:b00c::3"))
     self.assertTrue(not is_valid_ip("www.google.com"))
     self.assertTrue(not is_valid_ip("localhost"))
     self.assertTrue(not is_valid_ip("4.4.4.4<"))
     self.assertTrue(not is_valid_ip(" 127.0.0.1"))
     self.assertTrue(not is_valid_ip(""))
     self.assertTrue(not is_valid_ip(" "))
     self.assertTrue(not is_valid_ip("\n"))
     self.assertTrue(not is_valid_ip("\x00"))
开发者ID:Richard1021,项目名称:tornado,代码行数:13,代码来源:netutil_test.py

示例11: run

    async def run(self) -> None:
        try:
            self.parsed = urllib.parse.urlsplit(_unicode(self.request.url))
            if self.parsed.scheme not in ("http", "https"):
                raise ValueError("Unsupported url scheme: %s" % self.request.url)
            # urlsplit results have hostname and port results, but they
            # didn't support ipv6 literals until python 2.7.
            netloc = self.parsed.netloc
            if "@" in netloc:
                userpass, _, netloc = netloc.rpartition("@")
            host, port = httputil.split_host_and_port(netloc)
            if port is None:
                port = 443 if self.parsed.scheme == "https" else 80
            if re.match(r"^\[.*\]$", host):
                # raw ipv6 addresses in urls are enclosed in brackets
                host = host[1:-1]
            self.parsed_hostname = host  # save final host for _on_connect

            if self.request.allow_ipv6 is False:
                af = socket.AF_INET
            else:
                af = socket.AF_UNSPEC

            ssl_options = self._get_ssl_options(self.parsed.scheme)

            source_ip = None
            if self.request.network_interface:
                if is_valid_ip(self.request.network_interface):
                    source_ip = self.request.network_interface
                else:
                    raise ValueError(
                        "Unrecognized IPv4 or IPv6 address for network_interface, got %r"
                        % (self.request.network_interface,)
                    )

            timeout = min(self.request.connect_timeout, self.request.request_timeout)
            if timeout:
                self._timeout = self.io_loop.add_timeout(
                    self.start_time + timeout,
                    functools.partial(self._on_timeout, "while connecting"),
                )
                stream = await self.tcp_client.connect(
                    host,
                    port,
                    af=af,
                    ssl_options=ssl_options,
                    max_buffer_size=self.max_buffer_size,
                    source_ip=source_ip,
                )

                if self.final_callback is None:
                    # final_callback is cleared if we've hit our timeout.
                    stream.close()
                    return
                self.stream = stream
                self.stream.set_close_callback(self.on_connection_close)
                self._remove_timeout()
                if self.final_callback is None:
                    return
                if self.request.request_timeout:
                    self._timeout = self.io_loop.add_timeout(
                        self.start_time + self.request.request_timeout,
                        functools.partial(self._on_timeout, "during request"),
                    )
                if (
                    self.request.method not in self._SUPPORTED_METHODS
                    and not self.request.allow_nonstandard_methods
                ):
                    raise KeyError("unknown method %s" % self.request.method)
                for key in (
                    "proxy_host",
                    "proxy_port",
                    "proxy_username",
                    "proxy_password",
                    "proxy_auth_mode",
                ):
                    if getattr(self.request, key, None):
                        raise NotImplementedError("%s not supported" % key)
                if "Connection" not in self.request.headers:
                    self.request.headers["Connection"] = "close"
                if "Host" not in self.request.headers:
                    if "@" in self.parsed.netloc:
                        self.request.headers["Host"] = self.parsed.netloc.rpartition(
                            "@"
                        )[-1]
                    else:
                        self.request.headers["Host"] = self.parsed.netloc
                username, password = None, None
                if self.parsed.username is not None:
                    username, password = self.parsed.username, self.parsed.password
                elif self.request.auth_username is not None:
                    username = self.request.auth_username
                    password = self.request.auth_password or ""
                if username is not None:
                    assert password is not None
                    if self.request.auth_mode not in (None, "basic"):
                        raise ValueError(
                            "unsupported auth_mode %s", self.request.auth_mode
                        )
                    self.request.headers["Authorization"] = "Basic " + _unicode(
#.........这里部分代码省略.........
开发者ID:bdarnell,项目名称:tornado,代码行数:101,代码来源:simple_httpclient.py

示例12: validate_ip

 def validate_ip(cls, ip_address):
     ''' Checks the format of the string to confirm its a valid IPv4 or v6 address '''
     if netutil.is_valid_ip(ip_address):
         return ip_address
     else:
         raise ValueError("Invalid IP Address: '%s'" % str(ip_address))
开发者ID:CRYPTOlab,项目名称:RootTheBox,代码行数:6,代码来源:IpAddress.py


注:本文中的tornado.netutil.is_valid_ip函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。