mirror of
https://github.com/python/cpython.git
synced 2025-08-30 05:35:08 +00:00
Patch by Jp Calderone:
- The socket module now provides the functions inet_pton and inet_ntop for converting between string and packed representation of IP addresses. See SF patch #658327. This still needs a bit of work in the doc area, because it is not available on all platforms (especially not on Windows).
This commit is contained in:
parent
45f4130029
commit
47dfa4a89a
6 changed files with 221 additions and 1 deletions
|
@ -318,6 +318,65 @@ class GeneralModuleTests(unittest.TestCase):
|
|||
# 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('\x01\x02\x03\x04', f('1.2.3.4'))
|
||||
|
||||
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):
|
||||
try:
|
||||
from socket import inet_pton, AF_INET6, has_ipv6
|
||||
if not has_ipv6:
|
||||
return
|
||||
except ImportError:
|
||||
return
|
||||
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.2.3.4', f('\x01\x02\x03\x04'))
|
||||
|
||||
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):
|
||||
try:
|
||||
from socket import inet_ntop, AF_INET6, has_ipv6
|
||||
if not has_ipv6:
|
||||
return
|
||||
except ImportError:
|
||||
return
|
||||
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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue