#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    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:
    Code:
    #!/usr/bin/python
    import socket
    import sys
    
    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
    s.connect(server)
    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.connect(server)
    File "<string>", Line 1, in connect
    TypeError: an integer is required
    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?
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,647
    Rep Power
    1945
    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.
    Code:
    server = (ip, int(port))
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    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.

    Thanks again.
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,647
    Rep Power
    1945
    Originally Posted by wlandymore
    putting things like int before numbers
    Just to clarify. You are not putting int before numbers but before string/text to get a number.

    If you had something static like this:
    Code:
    port = 80
    you would not need to use int().
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0
    right.

    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.

IMN logo majestic logo threadwatch logo seochat tools logo