I am new to Python and programming. I am trying to code a simple script to scan for open ports.
Here is my code
import socket
ip = int(input("Please enter an ip address "))
file = open(‘ports.txt’, ‘r’)
port_list =
for line in file:
line_strip = line.strip()
line_split = line_strip.split()
port_list.append(line_split)
file.close()
print(port_list)
port_list_final =
for i in port_list:
for j in i:
port_list_final.append(j)
print(port_list_final)
for port in port_list_final:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = s.connect_ex((ip, port))
if result == 0:
print('Port', port, 'open')
else:
print('Port', port, 'closed')
I am getting the below error
Traceback (most recent call last):
File “C:\Users\crasto\PycharmProjects\pythonProject5\main.py”, line 2, in
ip = int(input("Please enter an ip address "))
ValueError: invalid literal for int() with base 10: ‘192.168.78.3’
Any idea how to fix this?
Many thanks in advance to anyone who is willing to help.
Here you read a string and you try to convert it to an integer:
As the error message ValueError: invalid literal for int() with base 10: ‘192.168.78.3’ suggests, the string 192.168.78.3 cannot be converted to an integer. The solution is to simply remove the converison (int()).
Conversion of an IP address to an unsigned 32-bit integer (in fact the native format of IP address) is certainly possible but it is not so simple as just calling the built-in int(). Anyway socket.connect_ex() probably does not accept an integer as an IP address.
Hi thanks so much for your reply.
I tried to just enter the ip address as a string and then I get this error instead
Traceback (most recent call last):
File “C:\Users\crasto\PycharmProjects\pythonProject5\main.py”, line 23, in
result = s.connect_ex((ip, port))
TypeError: an integer is required (got type str)
OK, now I see why you tried to convert the IP address. Unfortunately the error message fails to say which argument is str instead of the required int. As I checked your code, it is the port number - the second item in the tuple (ip, port).
You can try to check the documentation though I admit that it could be difficult for beginners as it is not as user-friendly as it could be:
socket.connect (address ) — points you to “address family — see above” again without a link
You have to find out that the mentioned “address families” you will find in Socket families
There you have to find AF_INET (and know that you have to look for AF_INET)
Another confusing part could be referring to a tuple as pair: “pair (host, port)”
Possible solution
It is very hard to read your code because you pasted it as markdown. Please enclose the code parts between triple backticks:
``` python
# your code here
```
You should convert your port numbers from strings probably in your second for loop. It looks like your ports.txt can contain multiple values on a line and you split them in the second loop.
Alternatively you can convert the type directly in the command with the method call:
Yeah you’re right it’s a bit tricky as I’m a beginner. The whole ‘family’ bit I don’t really understand it. But the possible solution is pretty clear I’m going to try that.
Thanks so very much for your help it’s much appreciated.
Oh and next time I’ll make sure I’ll paste my code the way you suggested.
By Dave via Discussions on Python.org at 16Jun2022 13:17:
The whole ‘family’ bit I don’t really understand it.
A “socket” is a generic thing for sending and receiving data. The
“family” is an indication of what domain this new socket it to work in.
You’re working with internet sockets. You can also use AF_UNIX to get
a “UNIX domain socket”, which is an entirely local thing which lives in
your local filesystem, for communicating between local processes without
using the internet at all.
You’re using IP addresses, so you want to use the AF_INET family.
I tried what you suggested and I get this error now
Traceback (most recent call last):
File “C:\Users\crasto\PycharmProjects\pythonProject5\main.py”, line 25, in
result = s.connect_ex((ip, port))
TypeError: str, bytes or bytearray expected, not int