1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Parsing IP address and port in python

Discussão em 'Python' iniciado por Stack, Outubro 5, 2024 às 10:52.

  1. Stack

    Stack Membro Participativo

    Using python, I'd like to accomplish two things:


    1. Need to split an ipv6 address and port combination in the format [fec2::10]:80 to fec2::10 and 80.


    2. Given an IP address and port combination, I need to determine if the IP is a v4 or v6 address. Eg: 1.2.3.4:80 and [fec2::10]:80

    Please suggest a way to do it.

    Thanks!

    Sample code:

    #!/usr/bin/env python


    def main():
    server = "[fec1::1]:80"
    if server.find("[", 0, 2) == -1:
    print "IPv4"
    ip, port = server.split(':')
    else:
    print "IPv6"
    new_ip, port = server.rsplit(':', 1)
    print new_ip
    ip = new_ip.strip('[]')

    print ip
    print port

    if __name__ == '__main__':
    main()


    This works for all cases except when the input is specified without a port. Eg: 10.78.49.50 and [fec2::10]

    Any suggestions to address this?

    Continue reading...

Compartilhe esta Página