Socket WinError 10022 but only as an executable on a Networkdrive

Viewed 50

I've got an XML file from which I parse a host adress + port. My script is supposed to establish a connection to every host over the given port (only port 22 ATM) to check the availability of the devices. Every host with which a connection is not possible is to be noted in a List.

file = minidom.parse('Sessions.XML')
sessiondata_all = file.getElementsByTagName('SessionData')
failed_ips = []

def check_port(host, port):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.bind(('', 22))
        sock.listen(1)
        sock.accept()
        if not sock.connect_ex((host, int(port))) == 0:
            failed_ips.append(host)
        sock.close()

def main():
    try:
        for sessiondata in sessiondata_all:
            host = sessiondata.attributes['Host'].value
            port = sessiondata.attributes['Port'].value
            print(host + ":" + port)
            check_port(host, port)
    except Exception as e:
        logging.error(traceback.format_exc())
    print(failed_ips)

if __name__ == '__main__':
    main()

This code is successful if I run it as a .py or .exe (pyinstaller --onefile [Filename.py]) locally. It also works on a networkdrive as a .py but as soon as I try it as an executable I get the following Error:

PS [Directory on the Networkdrive] .\FailedIps.exe
1.1.1.1
ERROR:root:Traceback (most recent call last):
  File "FailedIps.py", line 101, in main
  File "FailedIps.py", line 68, in check_port
  File "socket.py", line 232, in __init__
OSError: [WinError 10022] An invalid argument was supplied

[]

Anybody got an idea what's the matter here? I can't figure it out

Python and Pyinstaller are on current versions

1 Answers

You should swap out

socket.bind(('',22)) 

to

socket.bind((host,22)) 
Related