Split function for host:port in standard lib

M

Michael Ströder

HI!

Is there a function in the standard lib which can be used to split a
string containg 'host:port' into a tuple (host,port) and also does this
reliably for IPv6 addresses?

Ciao, Michael.
 
M

Manuel Ebert

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

AFAIK port names cannot contain any colons, so python2.5's
'host:port' .rsplit(':') should do the job. On < 2.5 you probably
need to do something like
s = addr.rindex(':')
host, port = addr[:s], addr[s+1:]

Best,
Manuel


HI!

Is there a function in the standard lib which can be used to split
a string containg 'host:port' into a tuple (host,port) and also
does this reliably for IPv6 addresses?

Ciao, Michael.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)

iD8DBQFIs/KjcZ70OCIgLecRAlfpAJ9aTgw5sADVKHXHTahzE+4zyuTZhACghNi6
eYyGqdIl8ONJxznwJYZ78Cc=
=s8rW
-----END PGP SIGNATURE-----
 
M

Michael Ströder

Manuel said:
Is there a function in the standard lib which can be used to split a
string containg 'host:port' into a tuple (host,port) and also does
this reliably for IPv6 addresses?
>
AFAIK port names cannot contain any colons, so python2.5's 'host:port'
.rsplit(':') should do the job. On < 2.5 you probably need to do
something like
s = addr.rindex(':')
host, port = addr[:s], addr[s+1:]

Manuel, thanks for answering. But I already thought about this approach.
I should have mentioned that I'd like to return None for a missing port
though. Note the :: in the IPv6 addresses.

Examples:
'localhost:389' -> ('localhost',389)
'localhost' -> ('localhost',None)

Examples IPv4 addresses:
'127.0.0.1' -> ('127.0.0.1',None)

Examples IPv6 addresses:
'::1:389' -> ('::1',389)
'::1' -> ('::1',None)

Ciao, Michael.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,630
Members
45,338
Latest member
41Pearline46

Latest Threads

Top