My concerns:
- Is this module thread safe?
- Is there any obvious performance issues I missed?
- Is there a better container type to use for
GENERATED_ADDRESSES
other than a list? - Is there any flags I can pass to
socket
to make the try /connect
any faster?
#-*-coding:utf8;-*-
#qpy:3
#qpy:console
import socket
import os
import random
import struct
import time
import threading
# This is used to set a default timeout on socket
# objects.
DEFAULT_TIMEOUT = 0.30
# This is used for checking if a call to socket.connect_ex
# was successful.
SUCCESS = 0
# GENERATED_ADDRESSES is used to store the addresses that
# generate_ip generates,
# The reason i have to log the addresses
# is because i don't want generate_ip to produce
# the same address twice.
# generate_ip will keep checking GENERATED_ADDRESSES for
# each addresses generated until it produces an address
# that's not in GENERATED_ADDRESSES.
GENERATED_ADDRESSES = []
LOCK = threading.Lock()
def elapsed(func):
def wraps(*args, **kw):
start = time.time()
retval = func(*args, **kw)
stop = time.time()
elap = stop - start
print(func.__name__, elap)
return retval
return wraps
def check_port(*host_port, timeout=DEFAULT_TIMEOUT):
''' Try to connect to a specified host on a specified port.
If the connection takes longer then the TIMEOUT we set we assume
the host is down. If the connection is a success we can safely assume
the host is up and listing on port x. If the connection fails for any
other reason we assume the host is down and the port is closed.'''
# Create and configure the socket.
sock = socket.socket()
sock.settimeout(timeout)
# the SO_REUSEADDR flag tells the kernel to reuse a local
# socket in TIME_WAIT state, without waiting for its natural
# timeout to expire.
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Like connect(address), but return an error indicator instead
# of raising an exception for errors returned by the C-level connect()
# call (other problems, such as “host not found,” can still raise exceptions).
# The error indicator is 0 if the operation succeeded, otherwise the value of
# the errnovariable. This is useful to support, for example, asynchronous connects.
opened = sock.connect_ex(host_port) is SUCCESS
# Mark the socket closed.
# The underlying system resource (e.g. a file descriptor)
# is also closed when all file objects from makefile() are closed.
# Once that happens, all future operations on the socket object will fail.
# The remote end will receive no more data (after queued data is flushed).
sock.close()
# return True if port is open or False if port is closed.
return opened
def is_private_ip(ip):
''' return True is ip is local and False if it is not.'''
return ipaddress.ip_address(ip).is_private
def random_ip(**kw):
while 1:
_ip = socket.inet_ntoa(struct.pack('>I', random.randint(1, 0xffffffff)))
if _ip not in GENERATED_ADDRESSES:
ip = _ip
# Are we running in a sub thread?
# If so lock while appending to GENERATED_ADDRESSES.
if threading.activeCount() > 1:
with LOCK:
GENERATED_ADDRESSES.append(ip)
# If running in the main thread we don't have to
# lock before appending to GENERATED_ADDRESSES.
else:
GENERATED_ADDRESSES.append(ip)
break
# Only produce private addresses?
if kw.get('private_only', 0) is True:
# Keep generating addresses until we generate
# a private address.
while 1:
if is_private_ip(ip):
return ip
# only produce public addresses?
if kw.get('public_only', 0) is True:
while 1:
# Keep generating addresses until we
# generate a private_address.
if not is_private_ip(ip):
return ip
# Just return the addresses regardless of it public or private
# orientation.
return ip
while 1:
host = random_ip(public_only=1)
opened = check_port(host, 80)
if opened:
print(host, len(GENERATED_ADDRESSES))