def test_getprotobyname(self): '''Tests _socket.getprotobyname''' #IP and CPython proto_map = { "icmp": _socket.IPPROTO_ICMP, "ip": _socket.IPPROTO_IP, "tcp": _socket.IPPROTO_TCP, "udp": _socket.IPPROTO_UDP, } #supported only by IP if is_cli: proto_map.update( {"dstopts": _socket.IPPROTO_DSTOPTS, "none": _socket.IPPROTO_NONE, "raw": _socket.IPPROTO_RAW, "ipv4": _socket.IPPROTO_IPV4, "ipv6": _socket.IPPROTO_IPV6, "esp": _socket.IPPROTO_ESP, "fragment": _socket.IPPROTO_FRAGMENT, "nd": _socket.IPPROTO_ND, "icmpv6": _socket.IPPROTO_ICMPV6, "routing": _socket.IPPROTO_ROUTING, "pup": _socket.IPPROTO_PUP, #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21918 "ggp": _socket.IPPROTO_GGP, #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21918 }) for proto_name, good_val in proto_map.iteritems(): temp_val = _socket.getprotobyname(proto_name) self.assertEqual(temp_val, good_val) #negative cases bad_list = ["", "blah", "i"] for name in bad_list: self.assertRaises(_socket.error, _socket.getprotobyname, name)
def test_getprotobyname(self): '''Tests _socket.getprotobyname''' #IP and CPython proto_map = { "icmp": _socket.IPPROTO_ICMP, "ip": _socket.IPPROTO_IP, "tcp": _socket.IPPROTO_TCP, "udp": _socket.IPPROTO_UDP, } if is_cli or sys.version_info >= (3, 8): proto_map.update({ "ipv6": _socket.IPPROTO_IPV6, "esp": _socket.IPPROTO_ESP, "pup": _socket. IPPROTO_PUP, #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21918 "ggp": _socket. IPPROTO_GGP, #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21918 }) proto_map_cli_only = { "dstopts": _socket.IPPROTO_DSTOPTS, "none": _socket.IPPROTO_NONE, "raw": _socket.IPPROTO_RAW, "ipv4": _socket.IPPROTO_IPV4, "fragment": _socket.IPPROTO_FRAGMENT, "nd": _socket.IPPROTO_ND, "icmpv6": _socket.IPPROTO_ICMPV6, "routing": _socket.IPPROTO_ROUTING, } for proto_name, good_val in proto_map_cli_only.items(): if is_cli: temp_val = _socket.getprotobyname(proto_name) self.assertEqual(temp_val, good_val) else: with self.assertRaises(OSError): _socket.getprotobyname(proto_name) for proto_name, good_val in proto_map.items(): temp_val = _socket.getprotobyname(proto_name) self.assertEqual(temp_val, good_val) #negative cases bad_list = ["", "blah", "i"] for name in bad_list: self.assertRaises(_socket.error, _socket.getprotobyname, name)
def create_socket(s, backlog=256, reuse_addr=None): if s[0] == 'tcp': family = _socket.AF_INET type = _socket.SOCK_STREAM proto = _socket.getprotobyname('tcp') elif s[0] == 'unix': family = _socket.AF_UNIX type = _socket.SOCK_STREAM proto = _socket.getprotobyname('unix') elif s[0] == 'udp': family = _socket.AF_INET type = _socket.SOCK_DGRAM proto = _socket.getprotobyname('udp') sock = _socket.socket(family=family,type=type,proto=proto) if reuse_addr is not None: sock.setsockopt(_socket.SOL_SOCKET, _socket.SO_REUSEADDR, reuse_addr) sock.bind((s[1],int(s[2]))) sock.listen(backlog) sock.setblocking(0) return sock