本文整理汇总了Python中gevent.socket.AF_INET属性的典型用法代码示例。如果您正苦于以下问题:Python socket.AF_INET属性的具体用法?Python socket.AF_INET怎么用?Python socket.AF_INET使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类gevent.socket
的用法示例。
在下文中一共展示了socket.AF_INET属性的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: bootstrap_from_dns
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import AF_INET [as 别名]
def bootstrap_from_dns(self):
"""
Fetches CM server list from WebAPI and replaces the current one
"""
self._LOG.debug("Attempting bootstrap via DNS")
try:
answer = socket.getaddrinfo("cm0.steampowered.com",
27017,
socket.AF_INET,
proto=socket.IPPROTO_TCP)
except Exception as exp:
self._LOG.error("DNS boostrap failed: %s" % str(exp))
return False
servers = list(map(lambda addr: addr[4], answer))
if servers:
self.clear()
self.merge_list(servers)
return True
else:
self._LOG.error("DNS boostrap: cm0.steampowered.com resolved no A records")
return False
示例2: gethostbyname_ex
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import AF_INET [as 别名]
def gethostbyname_ex(self, hostname, family=AF_INET):
if PY3:
if isinstance(hostname, str):
hostname = hostname.encode('idna')
elif not isinstance(hostname, (bytes, bytearray)):
raise TypeError('Expected es(idna), not %s' % type(hostname).__name__)
else:
if isinstance(hostname, text_type):
hostname = hostname.encode('ascii')
elif not isinstance(hostname, str):
raise TypeError('Expected string, not %s' % type(hostname).__name__)
while True:
ares = self.ares
try:
waiter = Waiter(self.hub)
ares.gethostbyname(waiter, hostname, family)
result = waiter.get()
if not result[-1]:
raise gaierror(-5, 'No address associated with hostname')
return result
except gaierror:
if ares is self.ares:
raise
# "self.ares is not ares" means channel was destroyed (because we were forked)
示例3: __init__
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import AF_INET [as 别名]
def __init__(self, config):
UpstreamBase.__init__(self, config)
self.socks5_hostname = config.get('host')
self.socks5_port = config.get('port')
if self.socks5_hostname is None or self.socks5_port is None:
ms = u'[配置错误] host、port 不能为空! upstream-type:%s' % self.type
raise ConfigError(ms)
class socket(SocketBase):
# TODO: 停掉一些不支持方法。
def __init__(self, family=_socket.AF_INET, type=_socket.SOCK_STREAM, proto=0, _sock=None):
if _sock is None:
_sock = socket.upstream.socket(family=family, type=type, proto=proto)
SocketBase.__init__(self, _sock)
socket.socks5_hostname = self.socks5_hostname
socket.socks5_port = self.socks5_port
socket.upstream = self.upstream
socket.display_name = self.get_display_name()
socket.name = self.get_name()
self.socket = socket
示例4: gethostbyname
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import AF_INET [as 别名]
def gethostbyname(self, hostname, family=AF_INET):
hostname = _resolve_special(hostname, family)
return self.gethostbyname_ex(hostname, family)[-1][0]
示例5: _getnameinfo
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import AF_INET [as 别名]
def _getnameinfo(self, sockaddr, flags):
if not isinstance(flags, int):
raise TypeError('an integer is required')
if not isinstance(sockaddr, tuple):
raise TypeError('getnameinfo() argument 1 must be a tuple')
address = sockaddr[0]
if not PY3 and isinstance(address, text_type):
address = address.encode('ascii')
if not isinstance(address, string_types):
raise TypeError('sockaddr[0] must be a string, not %s' % type(address).__name__)
port = sockaddr[1]
if not isinstance(port, int):
raise TypeError('port must be an integer, not %s' % type(port))
waiter = Waiter(self.hub)
result = self._getaddrinfo(address, str(sockaddr[1]), family=AF_UNSPEC, socktype=SOCK_DGRAM)
if not result:
reraise(*sys.exc_info())
elif len(result) != 1:
raise error('sockaddr resolved to multiple addresses')
family, socktype, proto, name, address = result[0]
if family == AF_INET:
if len(sockaddr) != 2:
raise error("IPv4 sockaddr must be 2 tuple")
elif family == AF_INET6:
address = address[:2] + sockaddr[2:]
self.ares.getnameinfo(waiter, address, flags)
node, service = waiter.get()
if service is None:
service = '0'
return node, service
示例6: get_ip_address
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import AF_INET [as 别名]
def get_ip_address():
# connecting to a UDP address doesn't send packets
try: return [(s.connect(('1.1.1.1', 0)), s.getsockname()[0], s.close()) for s in [socket(AF_INET, SOCK_DGRAM)]][0][1]
except:
logger.error('Network is unreachable')
sys.exit()
示例7: connect
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import AF_INET [as 别名]
def connect(self, host=None, port=None, address_family=None):
"""
Method that initiates a connection to an EPP host
"""
host = host or self.host
self.sock = socket.socket(address_family or socket.AF_INET, socket.SOCK_STREAM)
self.sock.settimeout(self.socket_connect_timeout) # connect timeout
self.sock.connect((host, port or self.port))
local_sock_addr = self.sock.getsockname()
local_addr, local_port = local_sock_addr[:2]
self.log.debug('connected local=%s:%s remote=%s:%s',
local_addr, local_port, self.sock.getpeername()[0], port)
self.sock.settimeout(self.socket_timeout) # regular timeout
if self.ssl_enable:
self.sock = ssl.wrap_socket(self.sock, self.keyfile, self.certfile,
ssl_version=self.ssl_version,
ciphers=self.ssl_ciphers,
server_side=False,
cert_reqs=self.cert_required,
ca_certs=self.cacerts)
self.log.debug('%s negotiated with local=%s:%s remote=%s:%s', self.sock.version(),
local_addr, local_port, self.sock.getpeername()[0], port)
if self.validate_hostname:
try:
match_hostname(self.sock.getpeercert(), host)
except CertificateError as exp:
self.log.exception("SSL hostname mismatch")
raise EppConnectionError(str(exp))
self.greeting = EppResponse.from_xml(self.read().decode('utf-8'))
示例8: __init__
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import AF_INET [as 别名]
def __init__(self,config):
u""" 初始化直连 socket 环境 """
UpstreamBase.__init__(self,config=config)
self.source_ip = config.get('source_ip','0.0.0.0')
self.source_port = config.get('source_port',0)
self.route_cache = LRUCacheDict(500, 10 * 60 * 1000)
if self.source_ip == '0.0.0.0' and self.source_port==0:
self.source_address = None
else:
self.source_address=(self.source_ip,self.source_port)
class socket(SocketBase):
def __init__(self, family=_socket.AF_INET, type=_socket.SOCK_STREAM, proto=0,_sock=None):
if _sock is None:
_sock = socket.upsocket.socket(family=family,type=type,proto=proto)
_sock.bind(self.source_address)
SocketBase.__init__(self,_sock)
socket.source_address = self.source_address
socket.upstream = self.upstream
socket.display_name = self.get_display_name()
socket.name = self.get_name()
self.socket = socket
示例9: __init__
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import AF_INET [as 别名]
def __init__(self):
ip = config("cuckoo:resultserver:ip")
port = config("cuckoo:resultserver:port")
pool_size = config("cuckoo:resultserver:pool_size")
sock = gevent.socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
sock.bind((ip, port))
except (OSError, socket.error) as e:
if e.errno == errno.EADDRINUSE:
raise CuckooCriticalError(
"Cannot bind ResultServer on port %d "
"because it was in use, bailing." % port
)
elif e.errno == errno.EADDRNOTAVAIL:
raise CuckooCriticalError(
"Unable to bind ResultServer on %s:%s %s. This "
"usually happens when you start Cuckoo without "
"bringing up the virtual interface associated with "
"the ResultServer IP address. Please refer to "
"https://cuckoo.sh/docs/faq/#troubles-problem "
"for more information." % (ip, port, e)
)
else:
raise CuckooCriticalError(
"Unable to bind ResultServer on %s:%s: %s" % (ip, port, e)
)
# We allow user to specify port 0 to get a random port, report it back
# here
_, self.port = sock.getsockname()
sock.listen(128)
self.thread = threading.Thread(
target=self.create_server, args=(sock, pool_size)
)
self.thread.daemon = True
self.thread.start()
示例10: gethostbyname_ex
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import AF_INET [as 别名]
def gethostbyname_ex(self, hostname, family=AF_INET):
if PY3:
if isinstance(hostname, str):
hostname = hostname.encode('idna')
elif not isinstance(hostname, (bytes, bytearray)):
raise TypeError('Expected es(idna), not %s' % type(hostname).__name__)
else:
if isinstance(hostname, text_type):
hostname = hostname.encode('ascii')
elif not isinstance(hostname, str):
raise TypeError('Expected string, not %s' % type(hostname).__name__)
while True:
ares = self.ares
try:
waiter = Waiter(self.hub)
ares.gethostbyname(waiter, hostname, family)
result = waiter.get()
if not result[-1]:
raise gaierror(-5, 'No address associated with hostname')
return result
except gaierror:
if ares is self.ares:
if hostname == b'255.255.255.255':
# The stdlib handles this case in 2.7 and 3.x, but ares does not.
# It is tested by test_socket.py in 3.4.
# HACK: So hardcode the expected return.
return ('255.255.255.255', [], ['255.255.255.255'])
raise
# "self.ares is not ares" means channel was destroyed (because we were forked)
示例11: _getnameinfo
# 需要导入模块: from gevent import socket [as 别名]
# 或者: from gevent.socket import AF_INET [as 别名]
def _getnameinfo(self, sockaddr, flags):
if not isinstance(flags, int):
raise TypeError('an integer is required')
if not isinstance(sockaddr, tuple):
raise TypeError('getnameinfo() argument 1 must be a tuple')
address = sockaddr[0]
if not PY3 and isinstance(address, text_type):
address = address.encode('ascii')
if not isinstance(address, string_types):
raise TypeError('sockaddr[0] must be a string, not %s' % type(address).__name__)
port = sockaddr[1]
if not isinstance(port, int):
raise TypeError('port must be an integer, not %s' % type(port))
waiter = Waiter(self.hub)
result = self._getaddrinfo(address, str(sockaddr[1]), family=AF_UNSPEC, socktype=SOCK_DGRAM)
if not result:
reraise(*sys.exc_info())
elif len(result) != 1:
raise error('sockaddr resolved to multiple addresses')
family, _socktype, _proto, _name, address = result[0]
if family == AF_INET:
if len(sockaddr) != 2:
raise error("IPv4 sockaddr must be 2 tuple")
elif family == AF_INET6:
address = address[:2] + sockaddr[2:]
self.ares.getnameinfo(waiter, address, flags)
node, service = waiter.get()
if service is None:
if PY3:
# ares docs: "If the query did not complete
# successfully, or one of the values was not
# requested, node or service will be NULL ". Python 2
# allows that for the service, but Python 3 raises
# an error. This is tested by test_socket in py 3.4
err = gaierror('nodename nor servname provided, or not known')
err.errno = 8
raise err
service = '0'
return node, service