Thread: socket error

    #1
  1. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407

    socket error


    Can anyone tell me what this means
    Code:
    Traceback (most recent call last):
      File "prog/proxy.py", line 15, in ?
        s.connect((ip, int(ports)))  #Connect to the given ip & port numbers
      File "<string>", line 1, in connect
      File "C:\Python23\lib\socket.py", line 143, in _dummy
        raise error(EBADF, 'Bad file descriptor')
    socket.error: (9, 'Bad file descriptor')
    What im tring to do is create a program that checks an ip/port and make sure its a working ip by connecting to it and receiving data( or do you have a better way )
    Code:
    from socket import *
    
    fp        = open('c:\Python23\prog\data.txt', 'r')   #Open the file for reading.
    read      = fp.read(1025)      #Read the file upto 1025 chars.
    fp.close()
    splits    = read.split('\n')   #Split the file according to the \n ( new line character ).
    
    s = socket(AF_INET, SOCK_STREAM) #Start socket protocol.
    
    for i in range(len(splits[:-1])):
        ip, ports = splits[i].split(':') #Create tuple of ips and ports.
    
        s.connect((ip, int(ports)))  #Connect to the given ip & port numbers
        s.send("GET \HTTP\1.1\r\n")
        received = s.recv(1025)
        s.close()
        
        print received, '\n'
    Hopefully i wont figure it out before anyone postes
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    print out the strings read from the text file. I suspect that the problem is that the address has 'http://' at the start:


    Code:
    >>> from socket import *
    >>> s = socket(AF_INET, SOCK_STREAM)
    >>> s.connect(('www.python.org', 80))
    >>> s.close()
    >>> s.connect(('http://www.python.org', 80))
    Traceback (most recent call last):
      File "<interactive input>", line 1, in ?
      File "<string>", line 1, in connect
      File "C:\dev\Python23\lib\socket.py", line 143, in _dummy
        raise error(EBADF, 'Bad file descriptor')
    error: (9, 'Bad file descriptor')
    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Ignore the previous post - I was wrong. The problem is that you close the socket, then do another connect call on it. Once you close the socket then it is dead - you have to create a new one.

    Code:
    >>> s.connect(('www.python.org', 80))
    >>> s.close()
    >>> s.connect(('www.python.org', 80))
    Traceback (most recent call last):
      File "<interactive input>", line 1, in ?
      File "<string>", line 1, in connect
      File "C:\dev\Python23\lib\socket.py", line 143, in _dummy
        raise error(EBADF, 'Bad file descriptor')
    error: (9, 'Bad file descriptor')
    >>>
    You do also get an error if you have 'http://' at the start of the URL, but it throws a different exception:

    Code:
    >>> s = socket(AF_INET, SOCK_STREAM)
    >>> s.connect(('http://www.python.org', 80))
    Traceback (most recent call last):
      File "<interactive input>", line 1, in ?
      File "<string>", line 1, in connect
    gaierror: (7, 'getaddrinfo failed')
    Dave - The Developers' Coach
  6. #4
  7. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    yes all of that i know, see what im tring to do is loop through some ips to check them and make sure it works.. if it does work then continue to the next one if it doesnt work then show [no good] and continue, what is the best recommendation for me to do this
    Last edited by xlordt; May 9th, 2004 at 04:45 PM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    "My socket code is raising an exception, help"

    "You're closing the socket, then trying to connect it again. You can't do that - when closed, a socket is dead. You have to make a new one."

    "Yes, I know all that. How can I make my program work?"

  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Basically what needs to be done here is to put the whole socket handleing bit inside a loop. This way your creating a new socket for each check and your closing it at the end of the loop. you can then use a simple if to check if the socket connects successfully since i believe the connect() method returns a False value if the connection is refused .

    If not then you can always use a try-except block to do roughly the same thing.

    Are you getting an error from every IP or just the closed ones? In any case you should defintly include a check to see if the connection was accepted if you can

    Hope this helps some,

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    Originally Posted by sfb
    "My socket code is raising an exception, help"

    "You're closing the socket, then trying to connect it again. You can't do that - when closed, a socket is dead. You have to make a new one."

    "Yes, I know all that. How can I make my program work?"

    hah? sfb your getting it wrong, the program i can do, just the looping part that is messed do you know what is really wrong??

    netytan: no just the closed ones.. that code i posted was the wrong code.. kinda sorry.. here is the actual code
    Code:
    from socket import *
    class MySock:
    
          def __init__(self, host, ip):
    
               self.con = socket(AF_INET, SOCK_STREAM)
               try:
                      if self.con.connect((host, int(ip))):
                         print '%s:%s  --  Good' % ( host, ip )
                      else:
                         raise Exception, 'skipping to Bad Message'
                      self.con.close()
               except:
                     print '%s:%s  --  Bad' % ( host, ip )
               self.con.close()
    
    
    for x in file('c:\Python23\prog\proxy.txt'):
    
        (hosts, ips) = x.split(':')
        Tcp = MySock(hosts, ips)
    what i want it to do is if the ip is closed then continue to the next
    Last edited by xlordt; May 10th, 2004 at 11:11 AM.
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Not sure how your file is organised but you might try checking the input being passed to your class. Also not thinking you really need to be using a class here - you might try changing to a function just for preformance and style reasons.

    Can you give up a few example lines for your program?

    Mark.
    programming language development: www.netytan.com Hula

  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    here's a revised version...

    Code:
    from socket import *
    
    def hostest(host, port):
        basesocket = socket(AF_INET, SOCK_STREAM)
        try:
            basesocket.connect((host, int(port))):
            print '%s:%s - Good' % (host, port)
            basesocket.close()
        except:
            print '%s:%s - Bad' % (host, port)
    
    for line in file('c:\Python23\prog\data.txt'):
        host, port = line.split(':')
        hostest(host, port)
    Note: this is untested as of yet but it should work in theory .

    Mark.
    Last edited by netytan; May 10th, 2004 at 11:11 AM.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo