April 2nd, 2013, 09:02 AM
Problem connecting to server/port
I'm just learning Python and I'm working on a test where I have a web server running locally and I want to connect to port 80 on it and display the banner.
So far I have:
the code goes fine and displays the message about connecting but then when it gets to the bottom for the connection I get the following errors:
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print "Please enter an IP address"
ip = raw_input()
print "Please enter a port number"
port = raw_input()
server = ('ip',port)
print >> sys.stderr, 'connecting to port ' + port + ' on ' + ip
Can anyone give me some help on how you can make a simple connection to that open port and then use 'print' to spit out the banner info?
April 2nd, 2013, 10:45 AM
As the error message says, it require an integer.
If you look at the documentation for the connect function, you will see that the server address need the port to be an integer.
raw_input() return the value as a string.
Furthermore, I don't think you should add ' ' around ip, as this is already an string.
This should give us something like the below code. It is untested, but you can give it a try.
server = (ip, int(port))
April 2nd, 2013, 12:39 PM
okay, that makes sense.
Thanks for the help. I'm still getting used to putting things like int before numbers, but I do like the way Python seems to be more 'plain language' than some other ones I looked at.
April 2nd, 2013, 01:39 PM
Just to clarify. You are not putting int before numbers but before string/text to get a number.
Originally Posted by wlandymore
If you had something static like this:
you would not need to use int().
April 2nd, 2013, 01:54 PM
I ran into this before when I was just testing out adding a few numbers together.
So I had something like:
num1 = raw_input("enter a number")
num2 = raw_input("enter a second number")
total = num1 + num2
but at first I was getting just the two numbers merged together because it was treating them like strings. So I had to put the int in front of them and then it worked.