To create a Python script that connects to random IPv4 addresses on port 80 and collects Server: headers, you can use the socket library. Here's a script that does this:
python
import socket
import random
def get_header(response):
headers = response.headers
return headers.get('Server', 'Unknown')
ip_addresses = []
port = 80
while len(ip_addresses) < 10:
random_ip = socket.inet_aton(f'{random.randint(1, 255)}.{random.randint(1, 255)}.{random.randint(1, 255)}.{random.randint(1, 255)}')
socket.setdefaulttimeout(5)
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((random_ip, port))
response = s.recv(1024)
server_header = get_header(response)
ip_addresses.append(random_ip)
print(f'Connected to {random_ip} on port {port} - Server: {server_header}')
except socket.error:
print(f'Failed to connect to {random_ip} on port {port}')
print("Collected IP addresses:")
for ip in ip_addresses:
print(ip)
This script does the following:
Import the necessary libraries: socket for networking operations and random for generating random IP addresses.
Define a function get_header that extracts the Server: header from an HTTP response.
Initialize an empty list ip_addresses to store the collected IP addresses.
In a loop, generate random IP addresses using random.inet_aton().
For each IP address, try to connect to port 80 using a socket.AF_INET and socket.SOCK_STREAM socket.
If the connection is successful, receive the server's response and extract the Server: header.
Append the IP address to the ip_addresses list and print the connection information.
If the connection fails, print an error message.
After the loop, print the collected IP addresses.
Please note that this script may not work if there are firewalls or other obstacles preventing the connections.