472,142 Members | 1,276 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,142 software developers and data experts.

Split function for host:port in standard lib

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.
Aug 26 '08 #1
3 2950
-----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
On Aug 26, 2008, at 1:31 PM, Michael Ströder wrote:
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.
--
http://mail.python.org/mailman/listinfo/python-list
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)

iD8DBQFIs/KjcZ70OCIgLecRAlfpAJ9aTgw5sADVKHXHTahzE+4zyuTZhACg hNi6
eYyGqdIl8ONJxznwJYZ78Cc=
=s8rW
-----END PGP SIGNATURE-----
Aug 26 '08 #2
Manuel Ebert wrote:
On Aug 26, 2008, at 1:31 PM, Michael Ströder wrote:
>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.
Aug 26 '08 #3
Michael Ströder schrieb:
Examples IPv6 addresses:
'::1:389' -('::1',389)
'::1' -('::1',None)
These are wrong, see http://tools.ietf.org/html/rfc2732 ("Format for
Literal IPv6 Addresses in URL's§).

Correct formats are:
[::1]:389
[::1]
Aug 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by eight02645999 | last post: by
4 posts views Thread by Michael Yanowitz | last post: by
5 posts views Thread by ranishobha21 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.