L
L
Lizard2020-04-23 04:49:00
Python
Lizard, 2020-04-23 04:49:00

How to properly work with sockets in Python3?

Hello.
I have several ip's of different sites. Everyone has port 443 open.

45.60.40.164
185.71.67.88
185.71.67.56
212.5.66.66
178.248.232.33
185.203.72.185
185.62.200.245
217.73.62.241
212.193.152.179
104.16.199.133
178.248.232.221
178.248.232.181
95.101.172.6


In order to make sure that port 443 is open, I scan them with Nmap.

nmap -p 443 -iL result_ips.txt

Nmap prints out that most of the ip really has an open port 443. Now I want to test this in Python.

import time
import socket

start_time = time.time()

def scan_port(ip,port):
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  sock.settimeout(0.01)
  try:
     connect = sock.connect((ip,port))
     print(ip + port + ' open.')
     connect.close()
  except:
     pass

ports = ['443']

for port in ports:
    file_result_ips = open('result_ips.txt', 'r')
    for ip in file_result_ips:
        scan_port(ip,port)
        
file_result_ips.close()

print('')
print('%s seconds' % (time.time() - start_time))


Result:

0.0005757808685302734 seconds
And that's it. Why does it happen like that? If I just scan 1 ip address per 1000 ports then the information is correct and roughly similar to Nmap results.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question