本文整理汇总了Python中socket.SOL_TCP属性的典型用法代码示例。如果您正苦于以下问题:Python socket.SOL_TCP属性的具体用法?Python socket.SOL_TCP怎么用?Python socket.SOL_TCP使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类socket
的用法示例。
在下文中一共展示了socket.SOL_TCP属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _create_remote_socket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOL_TCP [as 别名]
def _create_remote_socket(self, ip, port):
addrs = socket.getaddrinfo(ip, port, 0, socket.SOCK_STREAM,
socket.SOL_TCP)
if len(addrs) == 0:
raise Exception("getaddrinfo failed for %s:%d" % (ip, port))
af, socktype, proto, canonname, sa = addrs[0]
if self._forbidden_iplist:
if common.to_str(sa[0]) in self._forbidden_iplist:
raise Exception('IP %s is in forbidden list, reject' %
common.to_str(sa[0]))
remote_sock = socket.socket(af, socktype, proto)
self._remote_sock = remote_sock
self._fd_to_handlers[remote_sock.fileno()] = self
remote_sock.setblocking(False)
remote_sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
return remote_sock
示例2: _get_addrinfo_list
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOL_TCP [as 别名]
def _get_addrinfo_list(hostname, port, is_secure, proxy):
phost, pport, pauth = get_proxy_info(
hostname, is_secure, proxy.host, proxy.port, proxy.auth, proxy.no_proxy)
try:
if not phost:
addrinfo_list = socket.getaddrinfo(
hostname, port, 0, 0, socket.SOL_TCP)
return addrinfo_list, False, None
else:
pport = pport and pport or 80
# when running on windows 10, the getaddrinfo used above
# returns a socktype 0. This generates an error exception:
#_on_error: exception Socket type must be stream or datagram, not 0
# Force the socket type to SOCK_STREAM
addrinfo_list = socket.getaddrinfo(phost, pport, 0, socket.SOCK_STREAM, socket.SOL_TCP)
return addrinfo_list, True, pauth
except socket.gaierror as e:
raise WebSocketAddressException(e)
示例3: open_socket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOL_TCP [as 别名]
def open_socket(self):
if self.useInetSocket:
this_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.SOL_TCP)
else:
this_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
if this_socket:
try:
if self.useInetSocket:
this_socket.connect((self.socketHost, self.socketPort))
else:
this_socket.connect(self.socket_name)
except:
this_socket.close()
return this_socket
示例4: test_tcp_no_delay
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOL_TCP [as 别名]
def test_tcp_no_delay(self):
def get_handler_socket():
# return the server's handler socket object
ioloop = IOLoop.instance()
for fd in ioloop.socket_map:
instance = ioloop.socket_map[fd]
if isinstance(instance, FTPHandler):
break
return instance.socket
s = get_handler_socket()
self.assertTrue(s.getsockopt(socket.SOL_TCP, socket.TCP_NODELAY))
self.client.quit()
self.server.handler.tcp_no_delay = False
self.client.connect(self.server.host, self.server.port)
self.client.sendcmd('noop')
s = get_handler_socket()
self.assertFalse(s.getsockopt(socket.SOL_TCP, socket.TCP_NODELAY))
示例5: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOL_TCP [as 别名]
def __init__(self, inSock, nodelay = True, littleEndian = True):
self.prefix_ = "" if littleEndian else ">"
self.sock_ = inSock
self.sock_.setblocking(0)
if nodelay:
self.sock_.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 0)
self.readTime = .001
self.gotSize_ = False
self.msgSize_ = 0
self.buf_ = ""
self.gotSize_ = False
self.incoming = collections.deque()
self.outgoing = collections.deque()
# element access functions
示例6: _create_remote_socket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOL_TCP [as 别名]
def _create_remote_socket(self, ip, port):
addrs = socket.getaddrinfo(ip, port, 0, socket.SOCK_STREAM, socket.SOL_TCP)
if len(addrs) == 0:
raise Exception("getaddrinfo failed for %s:%d" % (ip, port))
af, socktype, proto, canonname, sa = addrs[0]
if self._forbidden_iplist:
if common.to_str(sa[0]) in self._forbidden_iplist:
raise Exception('IP %s is in forbidden list, reject' %
common.to_str(sa[0]))
remote_sock = socket.socket(af, socktype, proto)
self._remote_sock = remote_sock
self._fd_to_handlers[remote_sock.fileno()] = self
remote_sock.setblocking(False)
remote_sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
return remote_sock
示例7: test_connect_tcp_keepalive_options
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOL_TCP [as 别名]
def test_connect_tcp_keepalive_options(event_loop):
conn = Connection(
loop=event_loop,
socket_keepalive=True,
socket_keepalive_options={
socket.TCP_KEEPIDLE: 1,
socket.TCP_KEEPINTVL: 1,
socket.TCP_KEEPCNT: 3,
},
)
await conn._connect()
sock = conn._writer.transport.get_extra_info('socket')
assert sock.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE) == 1
for k, v in (
(socket.TCP_KEEPIDLE, 1),
(socket.TCP_KEEPINTVL, 1),
(socket.TCP_KEEPCNT, 3),
):
assert sock.getsockopt(socket.SOL_TCP, k) == v
conn.disconnect()
示例8: _gethostbyaddr
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOL_TCP [as 别名]
def _gethostbyaddr(ip):
try:
dns.ipv6.inet_aton(ip)
sockaddr = (ip, 80, 0, 0)
family = socket.AF_INET6
except Exception:
sockaddr = (ip, 80)
family = socket.AF_INET
(name, port) = _getnameinfo(sockaddr, socket.NI_NAMEREQD)
aliases = []
addresses = []
tuples = _getaddrinfo(name, 0, family, socket.SOCK_STREAM, socket.SOL_TCP,
socket.AI_CANONNAME)
canonical = tuples[0][3]
for item in tuples:
addresses.append(item[4][0])
# XXX we just ignore aliases
return (canonical, aliases, addresses)
示例9: _socket_bind_addr
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOL_TCP [as 别名]
def _socket_bind_addr(self, sock, af):
bind_addr = ''
if self._bind and af == socket.AF_INET:
bind_addr = self._bind
elif self._bindv6 and af == socket.AF_INET6:
bind_addr = self._bindv6
else:
bind_addr = self._accept_address[0]
bind_addr = bind_addr.replace("::ffff:", "")
if bind_addr in self._ignore_bind_list:
bind_addr = None
if bind_addr:
local_addrs = socket.getaddrinfo(bind_addr, 0, 0, socket.SOCK_STREAM, socket.SOL_TCP)
if local_addrs[0][0] == af:
logging.debug("bind %s" % (bind_addr,))
try:
sock.bind((bind_addr, 0))
except Exception as e:
logging.warn("bind %s fail" % (bind_addr,))
示例10: _socket_bind_addr
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOL_TCP [as 别名]
def _socket_bind_addr(self, sock, af, is_relay):
bind_addr = ''
if self._bind and af == socket.AF_INET:
bind_addr = self._bind
elif self._bindv6 and af == socket.AF_INET6:
bind_addr = self._bindv6
bind_addr = bind_addr.replace("::ffff:", "")
if bind_addr in self._ignore_bind_list:
bind_addr = None
if is_relay:
bind_addr = None
if bind_addr:
local_addrs = socket.getaddrinfo(
bind_addr, 0, 0, socket.SOCK_STREAM, socket.SOL_TCP)
if local_addrs[0][0] == af:
logging.debug("bind %s" % (bind_addr,))
sock.bind((bind_addr, 0))
示例11: _socket_bind_addr
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOL_TCP [as 别名]
def _socket_bind_addr(self, sock, af):
bind_addr = ''
if self._bind and af == socket.AF_INET:
bind_addr = self._bind
elif self._bindv6 and af == socket.AF_INET6:
bind_addr = self._bindv6
else:
bind_addr = self._accept_address[0]
bind_addr = bind_addr.replace("::ffff:", "")
if bind_addr in self._ignore_bind_list:
bind_addr = None
if self._is_relay:
bind_addr = None
if bind_addr:
local_addrs = socket.getaddrinfo(
bind_addr, 0, 0, socket.SOCK_STREAM, socket.SOL_TCP)
if local_addrs[0][0] == af:
logging.debug("bind %s" % (bind_addr,))
sock.bind((bind_addr, 0))
示例12: _gethostbyaddr
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOL_TCP [as 别名]
def _gethostbyaddr(ip):
try:
addr = dns.ipv6.inet_aton(ip)
sockaddr = (ip, 80, 0, 0)
family = socket.AF_INET6
except:
sockaddr = (ip, 80)
family = socket.AF_INET
(name, port) = _getnameinfo(sockaddr, socket.NI_NAMEREQD)
aliases = []
addresses = []
tuples = _getaddrinfo(name, 0, family, socket.SOCK_STREAM, socket.SOL_TCP,
socket.AI_CANONNAME)
canonical = tuples[0][3]
for item in tuples:
addresses.append(item[4][0])
# XXX we just ignore aliases
return (canonical, aliases, addresses)
示例13: init
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOL_TCP [as 别名]
def init(self, channels):
global main_socket
if not main_socket:
main_socket = socket.socket()
main_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Disable Nagle's algorithm
main_socket.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
main_socket.bind(('', 2666))
main_socket.listen(1)
self.channels = list(channels.values())
threading.Thread(target=self.socket_thread).start()
# Add header and send to clients
示例14: server_bind
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOL_TCP [as 别名]
def server_bind(self):
sock = self.socket
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, True)
self.RequestHandlerClass.bufsize = sock.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
SocketServer.TCPServer.server_bind(self)
示例15: get_ip_address
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SOL_TCP [as 别名]
def get_ip_address(hostname: str, workaround127: bool = False, version: int = None) \
-> Union[ipaddress.IPv4Address, ipaddress.IPv6Address]:
"""
Returns the IP address for the given host. If you enable the workaround,
it will use a little hack if the ip address is found to be the loopback address.
The hack tries to discover an externally visible ip address instead (this only works for ipv4 addresses).
Set ipVersion=6 to return ipv6 addresses, 4 to return ipv4, 0 to let OS choose the best one or None to use config.PREFER_IP_VERSION.
"""
if not workaround127:
with contextlib.suppress(ValueError):
addr = ipaddress.ip_address(hostname)
return addr
def getaddr(ip_version):
if ip_version == 6:
family = socket.AF_INET6
elif ip_version == 4:
family = socket.AF_INET
elif ip_version == 0:
family = socket.AF_UNSPEC
else:
raise ValueError("unknown value for argument ipVersion.")
ip = socket.getaddrinfo(hostname or socket.gethostname(), 80, family, socket.SOCK_STREAM, socket.SOL_TCP)[0][4][0]
if workaround127 and (ip.startswith("127.") or ip == "0.0.0.0"):
return get_interface("4.2.2.2").ip
return ipaddress.ip_address(ip)
try:
if hostname and ':' in hostname and version is None:
version = 0
return getaddr(config.PREFER_IP_VERSION) if version is None else getaddr(version)
except socket.gaierror:
if version == 6 or (version is None and config.PREFER_IP_VERSION == 6):
raise socket.error("unable to determine IPV6 address")
return getaddr(0)