Added static method addrinfo()

This commit is contained in:
Vivien Alger 2011-06-29 10:19:52 +02:00
parent 30681a7018
commit 78697b9663
2 changed files with 21 additions and 14 deletions

View File

@ -140,6 +140,17 @@ Sec-WebSocket-Accept: %s\r
# #
# WebSocketServer static methods # WebSocketServer static methods
# #
@staticmethod
def addrinfo(host, port=None):
""" Resolve a host (and optional port) to an IPv4 or IPv6 address.
Returns: family, socktype, proto, canonname, sockaddr
"""
addrs = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM, socket.IPPROTO_TCP)
if not addrs:
raise Exception("Could resolve host '%s'" % self.target_host)
return addrs[0]
@staticmethod @staticmethod
def daemonize(keepfd=None, chdir='/'): def daemonize(keepfd=None, chdir='/'):
os.umask(0) os.umask(0)
@ -687,14 +698,12 @@ Sec-WebSocket-Accept: %s\r
is a WebSockets client then call new_client() method (which must is a WebSockets client then call new_client() method (which must
be overridden) for each new client connection. be overridden) for each new client connection.
""" """
for res in socket.getaddrinfo(self.listen_host, self.listen_port, 0, socket.SOCK_STREAM, 6): addr = WebSocketServer.addrinfo(self.listen_host, self.listen_port)
af, socktype, proto, canonname, sa = res lsock = socket.socket(addr[0], addr[1])
lsock = socket.socket(af, socktype) lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) lsock.bind((self.listen_host, self.listen_port))
lsock.bind((self.listen_host, self.listen_port)) lsock.listen(100)
lsock.listen(100)
break
if self.daemon: if self.daemon:
self.daemonize(keepfd=lsock.fileno(), chdir=self.web) self.daemonize(keepfd=lsock.fileno(), chdir=self.web)

View File

@ -150,12 +150,10 @@ Traffic Legend:
# Connect to the target # Connect to the target
self.msg("connecting to: %s:%s" % ( self.msg("connecting to: %s:%s" % (
self.target_host, self.target_port)) self.target_host, self.target_port))
for res in socket.getaddrinfo(self.target_host, self.target_port, 0, socket.SOCK_STREAM, 6): addr = WebSocketServer.addrinfo(self.target_host, self.target_port)
af, socktype, proto, canonname, sa = res tsock = socket.socket(addr[0], addr[1])
tsock = socket.socket(af, socktype) tsock.connect((self.target_host, self.target_port))
tsock.connect((self.target_host, self.target_port))
break
if self.verbose and not self.daemon: if self.verbose and not self.daemon:
print(self.traffic_legend) print(self.traffic_legend)