我们从Python开源项目中,提取了以下32个代码示例,用于说明如何使用socket.getservbyport()。
def portscanudp (port): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: global checklist con = s.connect((target, port)) k=0 for i in checklist: if str(port)==checklist[i]['port']: k=1 x = str(port) + ",," + str(socket.getservbyport(port, 'udp'))+",,"+'udp'+",,"+'old' if(k==0): x = str(port) + ',,' + str(socket.getservbyport(port, 'udp')) + ',,' + 'udp'+",,"+'new' list.append(x) con.close() except: pass
def remote_proto(self): """Identification of the protocol associated with the remote port. Equals ``remote_port`` if no protocol is associated with it. :type: :class:`str` or :class:`int` """ try: return socket.getservbyport(self.remote_port, 'tcp') except socket.error: return self.remote_port
def _getnameinfo(sockaddr, flags=0): host = sockaddr[0] port = sockaddr[1] if len(sockaddr) == 4: scope = sockaddr[3] family = socket.AF_INET6 else: scope = None family = socket.AF_INET tuples = _getaddrinfo(host, port, family, socket.SOCK_STREAM, socket.SOL_TCP, 0) if len(tuples) > 1: raise socket.error('sockaddr resolved to multiple addresses') addr = tuples[0][4][0] if flags & socket.NI_DGRAM: pname = 'udp' else: pname = 'tcp' qname = dns.reversename.from_address(addr) if flags & socket.NI_NUMERICHOST == 0: try: answer = _resolver.query(qname, 'PTR') hostname = answer.rrset[0].target.to_text(True) except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): if flags & socket.NI_NAMEREQD: raise socket.gaierror(socket.EAI_NONAME) hostname = addr if scope is not None: hostname += '%' + str(scope) else: hostname = addr if scope is not None: hostname += '%' + str(scope) if flags & socket.NI_NUMERICSERV: service = str(port) else: service = socket.getservbyport(port, pname) return (hostname, service)
def portscantcp (port): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: global checklist con = s.connect((target, port)) try: m=0 for i in checklist: if str(port)==checklist[i]['port']: m=1 x = str(port) + ',,' + str(socket.getservbyport(port, 'tcp')) + ',,' + 'tcp'+",,"+'old' if(m==0): x = str(port) + ',,' + str(socket.getservbyport(port, 'tcp')) + ',,' + 'tcp'+",,"+'new' list.append(x) except: n=0 for i in checklist: if str(port)==checklist[i]['port']: n=1 x = str(port) + ',,' + 'SERVICE_NOT_FOUND' + ',,' + 'tcp'+",,"+'old' if n==0: x = str(port) + ',,' + 'SERVICE_NOT_FOUND' + ',,' + 'tcp'+",,"+'new' list.append(x) con.close() except: pass
def find_service_name(): protocolname = 'tcp' for port in [80, 25]: print ("Port: %s => service name: %s" %(port, socket.getservbyport(port, protocolname))) print ("Port: %s => service name: %s" %(53, socket.getservbyport(53, 'udp')))
def testGetServBy(self): eq = self.assertEqual # Find one service that exists, then check all the related interfaces. # I've ordered this by protocols that have both a tcp and udp # protocol, at least for modern Linuxes. if (sys.platform.startswith('linux') or sys.platform.startswith('freebsd') or sys.platform.startswith('netbsd') or sys.platform == 'darwin'): # avoid the 'echo' service on this platform, as there is an # assumption breaking non-standard port/protocol entry services = ('daytime', 'qotd', 'domain') else: services = ('echo', 'daytime', 'domain') for service in services: try: port = socket.getservbyname(service, 'tcp') break except socket.error: pass else: raise socket.error # Try same call with optional protocol omitted port2 = socket.getservbyname(service) eq(port, port2) # Try udp, but don't barf it it doesn't exist try: udpport = socket.getservbyname(service, 'udp') except socket.error: udpport = None else: eq(udpport, port) # Now make sure the lookup by port returns the same service name eq(socket.getservbyport(port2), service) eq(socket.getservbyport(port, 'tcp'), service) if udpport is not None: eq(socket.getservbyport(udpport, 'udp'), service) # Make sure getservbyport does not accept out of range ports. self.assertRaises((OverflowError, ValueError), socket.getservbyport, -1) self.assertRaises((OverflowError, ValueError), socket.getservbyport, 65536)
def testGetServBy(self): eq = self.assertEqual # Find one service that exists, then check all the related interfaces. # I've ordered this by protocols that have both a tcp and udp # protocol, at least for modern Linuxes. if (sys.platform.startswith('linux') or sys.platform.startswith('freebsd') or sys.platform.startswith('netbsd') or sys.platform == 'darwin'): # avoid the 'echo' service on this platform, as there is an # assumption breaking non-standard port/protocol entry services = ('daytime', 'qotd', 'domain') else: services = ('echo', 'daytime', 'domain') for service in services: try: port = socket.getservbyname(service, 'tcp') break except socket.error: pass else: raise socket.error # Try same call with optional protocol omitted port2 = socket.getservbyname(service) eq(port, port2) # Try udp, but don't barf if it doesn't exist try: udpport = socket.getservbyname(service, 'udp') except socket.error: udpport = None else: eq(udpport, port) # Now make sure the lookup by port returns the same service name eq(socket.getservbyport(port2), service) eq(socket.getservbyport(port, 'tcp'), service) if udpport is not None: eq(socket.getservbyport(udpport, 'udp'), service) # Make sure getservbyport does not accept out of range ports. self.assertRaises(OverflowError, socket.getservbyport, -1) self.assertRaises(OverflowError, socket.getservbyport, 65536)
def testGetServBy(self): eq = self.assertEqual # Find one service that exists, then check all the related interfaces. # I've ordered this by protocols that have both a tcp and udp # protocol, at least for modern Linuxes. if (sys.platform.startswith(('freebsd', 'netbsd')) or sys.platform in ('linux', 'darwin')): # avoid the 'echo' service on this platform, as there is an # assumption breaking non-standard port/protocol entry services = ('daytime', 'qotd', 'domain') else: services = ('echo', 'daytime', 'domain') for service in services: try: port = socket.getservbyname(service, 'tcp') break except socket.error: pass else: raise socket.error # Try same call with optional protocol omitted port2 = socket.getservbyname(service) eq(port, port2) # Try udp, but don't barf if it doesn't exist try: udpport = socket.getservbyname(service, 'udp') except socket.error: udpport = None else: eq(udpport, port) # Now make sure the lookup by port returns the same service name eq(socket.getservbyport(port2), service) eq(socket.getservbyport(port, 'tcp'), service) if udpport is not None: eq(socket.getservbyport(udpport, 'udp'), service) # Make sure getservbyport does not accept out of range ports. self.assertRaises(OverflowError, socket.getservbyport, -1) self.assertRaises(OverflowError, socket.getservbyport, 65536)
def testGetServBy(self): eq = self.assertEqual # Find one service that exists, then check all the related interfaces. # I've ordered this by protocols that have both a tcp and udp # protocol, at least for modern Linuxes. if (sys.platform.startswith('linux') or sys.platform.startswith('freebsd') or sys.platform.startswith('netbsd') or sys.platform == 'darwin'): # avoid the 'echo' service on this platform, as there is an # assumption breaking non-standard port/protocol entry services = ('daytime', 'qotd', 'domain') else: services = ('echo', 'daytime', 'domain') for service in services: try: port = socket.getservbyname(service, 'tcp') break except socket.error: pass else: raise socket.error # Try same call with optional protocol omitted port2 = socket.getservbyname(service) eq(port, port2) # Try udp, but don't barf if it doesn't exist try: udpport = socket.getservbyname(service, 'udp') except socket.error: udpport = None else: eq(udpport, port) # Now make sure the lookup by port returns the same service name eq(socket.getservbyport(port2), service) eq(socket.getservbyport(port, 'tcp'), service) if udpport is not None: eq(socket.getservbyport(udpport, 'udp'), service) # Make sure getservbyport does not accept out of range ports. self.assertRaises((OverflowError, ValueError), socket.getservbyport, -1) self.assertRaises((OverflowError, ValueError), socket.getservbyport, 65536)
def testGetServBy(self): eq = self.assertEqual # Find one service that exists, then check all the related interfaces. # I've ordered this by protocols that have both a tcp and udp # protocol, at least for modern Linuxes. if (sys.platform.startswith(('freebsd', 'netbsd', 'gnukfreebsd')) or sys.platform in ('linux', 'darwin')): # avoid the 'echo' service on this platform, as there is an # assumption breaking non-standard port/protocol entry services = ('daytime', 'qotd', 'domain') else: services = ('echo', 'daytime', 'domain') for service in services: try: port = socket.getservbyname(service, 'tcp') break except OSError: pass else: raise OSError # Try same call with optional protocol omitted port2 = socket.getservbyname(service) eq(port, port2) # Try udp, but don't barf if it doesn't exist try: udpport = socket.getservbyname(service, 'udp') except OSError: udpport = None else: eq(udpport, port) # Now make sure the lookup by port returns the same service name eq(socket.getservbyport(port2), service) eq(socket.getservbyport(port, 'tcp'), service) if udpport is not None: eq(socket.getservbyport(udpport, 'udp'), service) # Make sure getservbyport does not accept out of range ports. self.assertRaises(OverflowError, socket.getservbyport, -1) self.assertRaises(OverflowError, socket.getservbyport, 65536)
def animate(): for d in itertools.cycle(['|', '/', '-', '\\']): if done: break conn = socket.getservbyport(int(port)) sys.stdout.write('\r' + conn + ':' + ' ' + d) sys.stdout.flush() time.sleep(0.1) conn = socket.getservbyport(int(port)) sys.stdout.write('\r' + conn + ':' + ' ' + '[YES]\n')
def _getnameinfo(sockaddr, flags=0): host = sockaddr[0] port = sockaddr[1] if len(sockaddr) == 4: scope = sockaddr[3] family = socket.AF_INET6 else: scope = None family = socket.AF_INET tuples = _getaddrinfo(host, port, family, socket.SOCK_STREAM, socket.SOL_TCP, 0) if len(tuples) > 1: raise socket.error('sockaddr resolved to multiple addresses') addr = tuples[0][4][0] if flags & socket.NI_DGRAM: pname = 'udp' else: pname = 'tcp' qname = reversename.from_address(addr) if flags & socket.NI_NUMERICHOST == 0: try: answer = _resolver.query(qname, 'PTR') hostname = answer.rrset[0].target.to_text(True) except (resolver.NXDOMAIN, resolver.NoAnswer): if flags & socket.NI_NAMEREQD: raise socket.gaierror(socket.EAI_NONAME) hostname = addr if scope is not None: hostname += '%' + str(scope) else: hostname = addr if scope is not None: hostname += '%' + str(scope) if flags & socket.NI_NUMERICSERV: service = str(port) else: service = socket.getservbyport(port, pname) return (hostname, service)
def is_iana_reserved_port(port, protocol): """ Check whether the port is reserved by IANA Args: port (int): port protocol (str): application protocol (tcp or udp) Return: bool: whether the port is reserved by IANA """ try: socket.getservbyport(port, protocol) return True except socket.error: return False
def find_service_name(): protocolname = 'tcp' for port in [80, 25]: print "Port: %s => service name: %s" %(port, socket.getservbyport(port, protocolname)) print "Port: %s => service name: %s" %(53, socket.getservbyport(53, 'udp'))
def get_service(conn): dst_port = conn.dst_port proto = conn.proto try: return ' ({}) '.format(getservbyport(int(dst_port), proto)) except: return ''
def test_permit_privileged_ports(self): # Test FTPHandler.permit_privileged_ports_active attribute # try to bind a socket on a privileged port sock = None for port in reversed(range(1, 1024)): try: socket.getservbyport(port) except socket.error: # not registered port; go on try: sock = socket.socket(self.client.af, socket.SOCK_STREAM) self.addCleanup(sock.close) sock.settimeout(TIMEOUT) sock.bind((HOST, port)) break except socket.error as err: if err.errno == errno.EACCES: # root privileges needed if sock is not None: sock.close() sock = None break sock.close() continue else: # registered port found; skip to the next one continue else: # no usable privileged port was found sock = None with self.server.lock: self.server.handler.permit_privileged_ports = False self.assertRaises(ftplib.error_perm, self.client.sendport, HOST, port) if sock: port = sock.getsockname()[1] with self.server.lock: self.server.handler.permit_privileged_ports = True sock.listen(5) sock.settimeout(TIMEOUT) self.client.sendport(HOST, port) s, addr = sock.accept() s.close()