socket.inet_ntop, and pton question

Discussion in 'Python' started by Andrew, Jan 28, 2007.

  1. Andrew

    Andrew Guest

    Hi

    Are these functions (inet_ntop(), inet_pton()) from the socket library
    supported on Windows.

    If not is there an equivalent for them using Windows

    Ive seen mention of people creating their own in order to use them

    Appreciate the help

    ty
    Andrew, Jan 28, 2007
    #1
    1. Advertising

  2. Andrew wrote:
    > Hi
    >
    > Are these functions (inet_ntop(), inet_pton()) from the socket library
    > supported on Windows.
    >
    > If not is there an equivalent for them using Windows
    >
    > Ive seen mention of people creating their own in order to use them
    >
    > Appreciate the help
    >
    > ty


    Why didn't you just try:

    [E:\Projects]python
    Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import socket
    >>> socket.inet_aton("127.0.0.1")

    '\x7f\x00\x00\x01'
    >>> socket.inet_ntoa(_)

    '127.0.0.1'
    >>>



    This is on windows xp.

    --Irmen
    Irmen de Jong, Jan 28, 2007
    #2
    1. Advertising

  3. At Sunday 28/1/2007 15:17, Irmen de Jong wrote:

    > > Are these functions (inet_ntop(), inet_pton()) from the socket library
    > > supported on Windows.

    >
    >Why didn't you just try:
    >
    >[E:\Projects]python
    >Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
    >(Intel)] on win32
    >Type "help", "copyright", "credits" or "license" for more information.
    > >>> import socket
    > >>> socket.inet_aton("127.0.0.1")

    >'\x7f\x00\x00\x01'
    > >>> socket.inet_ntoa(_)

    >'127.0.0.1'
    > >>>


    But these are not the requested functions, inet_ntop() and inet_pton():

    py> socket.inet_ntop
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    AttributeError: 'module' object has no attribute 'inet_ntop'


    --
    Gabriel Genellina
    Softlab SRL






    __________________________________________________
    Preguntá. Respondé. Descubrí.
    Todo lo que querías saber, y lo que ni imaginabas,
    está en Yahoo! Respuestas (Beta).
    ¡Probalo ya!
    http://www.yahoo.com.ar/respuestas
    Gabriel Genellina, Jan 29, 2007
    #3
  4. Gabriel Genellina wrote:


    > But these are not the requested functions, inet_ntop() and inet_pton():
    >
    > py> socket.inet_ntop
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > AttributeError: 'module' object has no attribute 'inet_ntop'
    >
    >


    Oops, my bad. Should have had more coffee before replying I guess.

    --Irmen
    Irmen de Jong, Jan 29, 2007
    #4
  5. Andrew

    Andrew Guest

    Andrew, Jan 31, 2007
    #5
  6. Martin-298 wrote:
    >
    > Hi
    >
    > Are these functions (inet_ntop(), inet_pton()) from the socket library
    > supported on Windows.
    >
    > If not is there an equivalent for them using Windows
    >
    > Ive seen mention of people creating their own in order to use them
    >
    > Appreciate the help
    >
    > ty
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    >


    You can use the below code:
    def inet_ntop(address_family, packed_ip):
    if address_family != AF_INET:
    raise socket.error, (97, 'Address family not supported by protocol')
    lIP = []
    for ch in packed_ip:
    lIP.append(str(ord(ch)))
    strIP = string.join(lIP,'.')
    return strIP

    def inet_pton(address_family, ip_string):
    if address_family != AF_INET:
    raise socket.error, (97, 'Address family not supported by protocol')
    lIP = ip_string.split('.')
    strHexIP = ""
    for i in lIP:
    if i == '':
    continue
    strHex = "%x" % int(i)
    strHex = strHex.zfill(2)
    strHexIP += "\\x"+strHex
    return strHexIP

    --
    View this message in context: http://www.nabble.com/socket.inet_ntop,-and-pton-question-tp8677935p24823395.html
    Sent from the Python - python-list mailing list archive at Nabble.com.
    Mahesh Poojary S, Aug 5, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,822
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    966
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    545
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    662
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    715
Loading...

Share This Page