Certainly! Here's a Python script that connects to random IPv4 addresses on port 80, collects the Server: headers, and computes statistics:
python
import random
import socket
# Number of random IP addresses to connect
num_connections = 10
# List to store server headers
server_headers = []
# Function to connect to a random IP address and collect server header
def connect_and_collect_header():
# Generate a random IP address
ip_address = ".".join(str(random.randint(0, 255)) for _ in range(4))
try:
# Connect to the IP address on port 80
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
s.connect((ip_address, 80))
# Send a basic HTTP request
s.sendall(b"GET / HTTP/1.1\r\nHost: " + ip_address.encode() + b"\r\n\r\n")
# Receive the response
response = s.recv(4096).decode()
# Extract the server header from the response
server_header = response.split("Server:", 1)[-1].split("\r\n", 1)[0].strip()
# Add the server header to the list
server_headers.append(server_header)
# Close the socket connection
s.close()
except Exception as e:
# Handle any errors that occurred during the connection
print(f"Error connecting to {ip_address}: {str(e)}")
# Perform the connections and header collection
for _ in range(num_connections):
connect_and_collect_header()
# Compute statistics
num_total = len(server_headers)
num_unique = len(set(server_headers))
# Print the collected server headers and statistics
print("Collected Server Headers:")
for header in server_headers:
print(header)
print("\nStatistics:")
print(f"Total number of server headers collected: {num_total}")
print(f"Number of unique server headers collected: {num_unique}")
Note: This script uses the socket module to establish a TCP connection to random IPv4 addresses on port 80. It sends a basic HTTP GET request and extracts the Server: header from the response. The collected server headers are stored in a list, and statistics on the collected headers are computed and printed at the end.
Please keep in mind that connecting to random IP addresses can be potentially risky and may violate certain terms of service or laws. Ensure that you use this script responsibly and legally.