Python socket module - for loop - socket.gaierror: [Errno 11001] getaddrinfo failed

Asked

Viewed 93 times

1

I am trying to create a script that performs connection testing on various hosts and different ports, example:
10.20.20.1,1433
10.30.30.2,1521
10.30.30.3,80
10.20.20.4,443

In the script I open a file with the hosts, then open the file with the ports, and use a for for each item within the file. I’m getting the error screen below:

socket.gaierror: [Errno 11001] getaddrinfo failed

Does anyone know what might be causing this error?

Follows the script:

import socket

a_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

with open('hosts', 'r') as hosts:
    for host in hosts:
        with open('ports', 'r') as ports:
            for port in ports:
                intport = int(port)
                location = (host, intport)
                result_of_check = a_socket.connect_ex(location)
                if result_of_check == 0:
                    print("Port is open")
                else:
                    print("Port is not open")
                a_socket.close()
  • Which command are you using to run the script? Ports with values lower than 1024 require a privileged user to be able to be instained. Is trying with root user?

  • Oops, all right? The error already appears when running by Pycharm... and Pycharm I run in Adm mode.

  • Check your host files, because I made a modifying to run here using the question data and worked.

2 answers

0

sorry for the delay and thanks for your attention.

I performed some tests and it worked as follows my script:

import socket Devices = open('Devices') tcpports = open(ports') tcpportsread = tcpports.readlines()

for line in Devices: a = line.rstrip() for ports in tcpportsread: mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) a = line.rstrip() ports = int(ports) responsefromsvr = mysock.connect_ex((a, ports)) mysock.close() responsif efromsvr == 0: print('{} answered on port {}'.format(a, ports)) Else: print('{} NOT responding on port {}'. format(a, ports))

0

According to the documentation:

socket exception.gaierror

A subclass of Oserror, this exception is generated for errors related to addresses by getaddrinfo() and getnameinfo().

Oserror

"file not found" or "disk full" (not for types of unlawful arguments or other incidental errors).

The idea is that the script starts connection test.
There should be exceptions if there are addresses that are not waiting for connection.
If you are a local network and have machines with addresses and ports waiting for any kind of connection request, the attempt will occur (if you do not need a password)

If you only have the address of a machine and it is not with the address and the port waiting , prepared for connection will return an Exception.

If you have a client trying to connect to some address with the port and the other side not having a server waiting for Exception.

But if there is a service for example apache and try to use the script with the port and address will occur request and the server will send a positive response.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.