problem parsing an int from IRC output.....

Irmen de Jong irmen at -NOSPAM-REMOVETHIS-xs4all.nl
Wed May 14 18:42:33 EDT 2003


Luke Ordelmans wrote:

> but i fail trying to convert the PORT to int....
> 
> here's (a piece of) my code for this part:
> 
> if data.find("DCC CHAT chat") != -1:
>         servip = data.__getslice__(len(data)-19, len(data)-9)
>         servport = data.__getslice__(len(data)-8, len(data))  

__getslice__? Why use this when we have [n:m] ?

>         print("\tfrom IP: " + str(servip) )
>         print("\tat port: " + str(servport) )
>         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>         s.bind(("localhost", 9234))
>         #server = ("localhost", servport)                       # FAILES
>         #server = ("localhost", int(servport))                  # FAILES
>         server = ("localhost", int(servport.strip()), 0)        # FAILES
>         s.connect(server)
> 
> the error I get:
> 
> File "./FSStatus.py", line 28, in dataReceived
> server = ("localhost",int(servport))
> exceptions.ValueError: invalid literal for int(): 49369

My guess is that there are invalid characters in the string
you want to convert to an int. Because Python says so :-)
(even though they don't show up in the traceback, they
might still be there .)

I.e. the piece of code that slices out the servport part
of the data doesn't chop off everything that you want
chopped off...

Can you show us a piece of the input that you give this
code? When you run it yourself it must be fairly simple
to see what's wrong when you test it manually with the
exacy input. Also try printing the contents of servport;

print list(servport)  # this will show 'hidden' chars too

--Irmen





More information about the Python-list mailing list