1
#!/usr/bin/python3
import socket

ip = input('Enter the IP address: ')
port = input('Enter the Port: ')

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

if s.connect_ex((ip,port)):
        print ('Port', port, 'is closed')
else:
        print ('Port', port, 'is open')

As you can see, it is a very simply port scanner that I have attempted to run (currently learning some Penetration Testing).

This is the error I get:

Traceback (most recent call last):
  File "./python.py", line 9, in <module>
    if s.connect_ex((ip,port)):
TypeError: an integer is required (got type str)

I assume that I need to set line 9 as an integer - how do I do this?

Cheers in advance.

  • Thank you for posting a full error traceback. What do you think the error means? – quamrana Dec 18 '17 at 20:13
  • 3
    Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – Pavel Dec 18 '17 at 20:16
  • @quamrana I believe it means that I need to set line 9 as an integer. However, I don't know how to do this is such context. – Tom Piper Dec 18 '17 at 20:17
  • 1
    As you can see from the answers, its actually only complaining about the `port` parameter of the `connect_ex` method. That method requires an integer as the second parameter, but the more you learn about python and its libraries the easier it will become to diagnose these things. – quamrana Dec 18 '17 at 20:25
  • @quamrana Thank you for clarifying this - I've solved that issue now (time to deal with the next). Not sure if your first comment was being sarcastic. I.e. It's not that acceptable to post full error tracebacks. Sorry if it came accross as If I didn't want to put any effort in myself. – Tom Piper Dec 18 '17 at 20:33
  • Not at all sarcastic. Its a failing of many posters to post incomplete information. Your post was complete with code **and** a traceback. Good question. – quamrana Dec 18 '17 at 20:40
  • @quamrana Oh, good. :) Thanks for the help, anyway. – Tom Piper Dec 18 '17 at 20:46

3 Answers3

1

Here: port = input('Enter the Port: ') you read user input which is a str and you pass it as port number (which should be as you've probably already guessed an int). You traceback is telling you exactly the same thing. You need to convert the port to integer with calling int(port)

gonczor
  • 3,994
  • 1
  • 21
  • 46
0
port = int(input('Enter the Port: '))
Mike Delta
  • 726
  • 2
  • 16
  • 32
0

input() returns a string, which you need to convert into an integer. Converting strings to valid integers can be a rather complex task. For a first approach change

port = input('Enter the Port: ')

into

port = input('Enter the Port: ')
try:
    port = int(port)
except ValueError as err:
    print('Invalid input. Please try again.')
albert
  • 8,027
  • 10
  • 48
  • 84