Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 9th, 2004, 03:30 AM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonders.
Posts: 5,548 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 1 h 59 m 56 sec
Reputation Power: 378
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook MySpace
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

Reply With Quote
  #2  
Old May 9th, 2004, 07:00 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,226 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 23 h 35 m 17 sec
Reputation Power: 263
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

Reply With Quote
  #3  
Old May 9th, 2004, 07:05 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,226 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 23 h 35 m 17 sec
Reputation Power: 263
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

Reply With Quote
  #4  
Old May 9th, 2004, 03:27 PM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonders.
Posts: 5,548 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 1 h 59 m 56 sec
Reputation Power: 378
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook MySpace
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 03:45 PM.

Reply With Quote
  #5  
Old May 10th, 2004, 08:20 AM
sfb sfb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 447 sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 1 h 43 m 45 sec
Reputation Power: 9
"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?"


Reply With Quote
  #6  
Old May 10th, 2004, 09:08 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #7  
Old May 10th, 2004, 09:13 AM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonders.
Posts: 5,548 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 1 h 59 m 56 sec
Reputation Power: 378
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook MySpace
Quote:
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 10:11 AM.

Reply With Quote
  #8  
Old May 10th, 2004, 09:25 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #9  
Old May 10th, 2004, 09:42 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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 10:11 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > socket error


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |