Index: Lib/test/test_socket.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/test/test_socket.py,v retrieving revision 1.59 diff -u -r1.59 test_socket.py --- Lib/test/test_socket.py 16 Sep 2002 01:30:03 -0000 1.59 +++ Lib/test/test_socket.py 31 Dec 2002 16:48:18 -0000 @@ -310,6 +310,53 @@ # Check that setting it to an invalid type raises TypeError self.assertRaises(TypeError, socket.setdefaulttimeout, "spam") + def testIPv4toString(self): + from socket import inet_aton as f, inet_pton, AF_INET + g = lambda a: inet_pton(AF_INET, a) + + self.assertEquals('\x00\x00\x00\x00', f('0.0.0.0')) + self.assertEquals('\xff\x00\xff\x00', f('255.0.255.0')) + self.assertEquals('\xaa\xaa\xaa\xaa', f('170.170.170.170')) + + self.assertEquals('\x00\x00\x00\x00', g('0.0.0.0')) + self.assertEquals('\xff\x00\xff\x00', g('255.0.255.0')) + self.assertEquals('\xaa\xaa\xaa\xaa', g('170.170.170.170')) + + def testIPv6toString(self): + from socket import inet_pton, AF_INET6 + f = lambda a: inet_pton(AF_INET6, a) + + self.assertEquals('\x00' * 16, f('::')) + self.assertEquals('\x00' * 16, f('0::0')) + self.assertEquals('\x00\x01' + '\x00' * 14, f('1::')) + self.assertEquals( + '\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae', + f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae') + ) + + def testStringToIPv4(self): + from socket import inet_ntoa as f, inet_ntop, AF_INET + g = lambda a: inet_ntop(AF_INET, a) + + self.assertEquals('1.0.1.0', f('\x01\x00\x01\x00')) + self.assertEquals('170.85.170.85', f('\xaa\x55\xaa\x55')) + self.assertEquals('255.255.255.255', f('\xff\xff\xff\xff')) + + self.assertEquals('1.0.1.0', g('\x01\x00\x01\x00')) + self.assertEquals('170.85.170.85', g('\xaa\x55\xaa\x55')) + self.assertEquals('255.255.255.255', g('\xff\xff\xff\xff')) + + def testStringToIPv6(self): + from socket import inet_ntop, AF_INET6 + f = lambda a: inet_ntop(AF_INET6, a) + + self.assertEquals('::', f('\x00' * 16)) + self.assertEquals('::1', f('\x00' * 15 + '\x01')) + self.assertEquals( + 'aef:b01:506:1001:ffff:9997:55:170', + f('\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70') + ) + # XXX The following don't test module-level functionality... def testSockName(self): @@ -606,6 +653,7 @@ class SmallBufferedFileObjectClassTestCase(FileObjectClassTestCase): bufsize = 2 # Exercise the buffering code + def test_main(): suite = unittest.TestSuite() Index: Doc/lib/libsocket.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/lib/libsocket.tex,v retrieving revision 1.68 diff -u -r1.68 libsocket.tex --- Doc/lib/libsocket.tex 13 Jun 2002 15:07:43 -0000 1.68 +++ Doc/lib/libsocket.tex 31 Dec 2002 16:48:19 -0000 @@ -349,6 +349,37 @@ support. \end{funcdesc} +\begin{funcdesc}{inet_pton}{address_family, ip_string} +Convert an IP address from its family-specific string format to a packed, +binary format. + +Supported values for address_family are currently AF_INET and AF_INET6. + +\function{inet_pton()} is useful when a library or network protocol calls for +an object of type \ctype{struct in_addr} (similar to \function{inet_aton()}) +or \ctype{struct in6_addr}. + +If the IP address string passed to this function is invalid, +\exception{socket.error} will be raised. Note that exactly what is valid +depends on both the value of \var{address_family} and the underlying +implementation of \cfunction{inet_pton()}. +\end{funcdesc} + +\begin{funcdesc}{inet_ntop}{address_family, packed_ip} +Convert a packed IP address (a string of some number of characters) to its +standard, family-specific string representation (for example, '7.10.0.5' or +'5aef:2b::8') + +Supported values for address_family are currently AF_INET and AF_INET6. + +\function{inet_pton()} is useful when a library or network protocol calls for +an object of type \ctype{struct in_addr} (similar to \function{inet_aton()}) +or \ctype{struct in6_addr}. + +If the string passed to this function is not the correct length for the +specified address family, \exception{socket.error} will be raised. +\end{funcdesc} + \begin{datadesc}{SocketType} This is a Python type object that represents the socket object type. It is the same as \code{type(socket(...))}. Index: Misc/NEWS =================================================================== RCS file: /cvsroot/python/python/dist/src/Misc/NEWS,v retrieving revision 1.568 diff -u -r1.568 NEWS --- Misc/NEWS 23 Dec 2002 16:51:41 -0000 1.568 +++ Misc/NEWS 31 Dec 2002 16:48:21 -0000 @@ -345,6 +345,9 @@ Extension modules ----------------- +- The socket module now provides the functions inet_pton and inet_ntop + for converting between string and packed representation of IP addresses. + - The new datetime module supplies classes for manipulating dates and times. The basic design came from the Zope "fishbowl process", and favors practical commercial applications over calendar esoterica. See