How to speed up this IP sweep, Port scanner python I am looking for a way to speed up my basic IP scanner/port scanner in Python 3.8.
My IP portion works, the port scan work, I just want to speed it up, in a format that someone learning python would understand.
#testing scanner.py 
import socket
import subprocess
import sys
from datetime import datetime
def scan():
    # Ask for the IP address to scan, for this its a single IP
    remoteServer = input(&quot;Enter a Starting IP: &quot;)
    remoteServerend = input(&quot;Enter the Ending IP: &quot;)
    print (&quot;Starting Scan at: &quot;,remoteServer)
    print (&quot;Ending Scan at: &quot;,remoteServerend)
    # change DNS to IPmail
    #remoteServerIP = socket.gethostbyname(remoteServer)
    #print (&quot;remoteServerIP is &quot;,remoteServerIP)
    # Ask for starting port
    startport = int(input(&quot;What port number do you want to start with? &quot;))
    # Ask for ending port
    endport = int(input(&quot;What is the ending port number? &quot;))
    # Print 50 *'s to make a top border
    print (&quot;-&quot; * 50)
    # print wait here is the IP we are scanning
    print (&quot;Please wait, scanning remote host&quot;, remoteServer)
    # print the bottom border
    print (&quot;-&quot; * 50)
    # Check what time the scan started
    t1 = datetime.now()
    # lets try the try statment for some error handeling and see if it can help?
    try:
    # trying our 1st port to our last port and replying if it is open, we don't care about closed ports
        for port in range(startport,endport):
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            result = sock.connect_ex((remoteServer, port))
            if result == 0:
                print (&quot;Port {}:     Open&quot;.format(port))
            sock.close()
    # you got tied of waiting and quit
    except KeyboardInterrupt:
        print( &quot;You pressed Ctrl+C&quot;)
        sys.exit()
    # failed DNS lookup
    except socket.gaierror:
        print( 'Hostname could not be resolved. Exiting')
        sys.exit()
    # no route to server?
    except socket.error:
        print( &quot;Couldn't connect to server&quot;)
        sys.exit()
    # get ending time
    t2 = datetime.now()
    # calculate how long did the script run?
    total =  t2 - t1
    # Printing the information to screen
    print( 'Scanning Completed in: ', total)



scan()
IP range

for this its a single IP

It is? Then why do you ask for a range? Your 'end' is not used, so just delete it.
Fail-safe scanning
Consider rearranging this:
try:
    for port in range(startport,endport):

to:
for port in range(startport,endport):
    try:

In other words, some ports may fail and others may succeed.