patternpythonMinorCanonical
Python Port Scanner 2.1
Viewed 0 times
scannerpythonport
Problem
I made lots of changes to the script presented in my previous question. I was tempted to edit that one with the new code, but it would invalidate @200_success's helpful answer. It was also disallowed on a Meta thread. So I apologize for the new question, but it seemed like the right thing to do.
Again, any and all tips are appreciated! Also, this is my first time writing any docstrings, so if I'm breaking convention, let me know.
```
#!/usr/bin/env python3
import argparse
import errno
import functools
import multiprocessing
import os
import platform
import socket
import time
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ProcessPoolExecutor
DEFAULT_HOST = '127.0.0.1'
DEFAULT_TIMEOUT = 1
DEFAULT_THREADS = 512
PORT_RANGE = range(1, 65536)
def tcp_ping(host, port):
"""
Attempts to connect to host:port via TCP.
Arguments:
host: IP address or URL to hit
port: Port to hit
Returns:
port number, if it's available; otherwise False
"""
try:
with socket.socket() as sock:
sock.connect((host, port))
print(str(port) + ' Open')
return port
except socket.timeout:
return False
except socket.error as socket_error:
if socket_error.errno == errno.ECONNREFUSED:
return False
raise
def perform_scan(host, use_threads = False):
"""
Perform a scan on all valid ports (1 - 65535), either by
spawning threads or forking processes.
Arguments:
host: IP address or URL to scan
use_threads: whether or not to use threads; default
behaviour is to fork processes
Returns:
list of available ports
"""
if use_threads:
executor = ThreadPoolExecutor(max_workers = DEFAULT_THREADS)
else:
executor = ProcessPoolExecutor()
with executor:
ping_partial = functools.partial(tcp_ping, host)
return list(filter(bool, executor.ma
Again, any and all tips are appreciated! Also, this is my first time writing any docstrings, so if I'm breaking convention, let me know.
```
#!/usr/bin/env python3
import argparse
import errno
import functools
import multiprocessing
import os
import platform
import socket
import time
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ProcessPoolExecutor
DEFAULT_HOST = '127.0.0.1'
DEFAULT_TIMEOUT = 1
DEFAULT_THREADS = 512
PORT_RANGE = range(1, 65536)
def tcp_ping(host, port):
"""
Attempts to connect to host:port via TCP.
Arguments:
host: IP address or URL to hit
port: Port to hit
Returns:
port number, if it's available; otherwise False
"""
try:
with socket.socket() as sock:
sock.connect((host, port))
print(str(port) + ' Open')
return port
except socket.timeout:
return False
except socket.error as socket_error:
if socket_error.errno == errno.ECONNREFUSED:
return False
raise
def perform_scan(host, use_threads = False):
"""
Perform a scan on all valid ports (1 - 65535), either by
spawning threads or forking processes.
Arguments:
host: IP address or URL to scan
use_threads: whether or not to use threads; default
behaviour is to fork processes
Returns:
list of available ports
"""
if use_threads:
executor = ThreadPoolExecutor(max_workers = DEFAULT_THREADS)
else:
executor = ProcessPoolExecutor()
with executor:
ping_partial = functools.partial(tcp_ping, host)
return list(filter(bool, executor.ma
Solution
Trying to connect to TCP port 80 in order to check whether the DNS lookup succeeds is overkill. You should just call
socket.gethostbyname().Context
StackExchange Code Review Q#46866, answer score: 4
Revisions (0)
No revisions yet.