Getting information about ports, protocols, and domains

The socket module provides the socket.getservbyport(port[, protocol_name]) method, which allows us to get the port name from the port number. For example:

>>> import socket
>>> socket.getservbyport(80)
'http'
>>> socket.getservbyport(23)
'telnet'

We can also get information about the service name at the application level if we pass the protocol name as a second parameter.

You can find the following code in the socket_finding_service_name.py file:

#!/usr/bin/env python3
import socket

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')))

if __name__ == '__main__':
find_service_name()

This is the output of the previous script, where we can see the service name at the application level for each port:

Port: 80 => service name: http
Port: 25 => service name: smtp
Port: 53 => service name: domain

With the getaddrinfo() method, we can get information about the service that is working behind a domain. In this example, we are using this method to get the server behind the www.packtpub.com domain.

You can find the following code in the socket_getaddrinfo.py file:

#!/usr/bin/env python3

import socket

try:
infolist = socket.getaddrinfo('www.packtpub.com', 'www', 0, socket.SOCK_STREAM, 0, socket.AI_ADDRCONFIG | socket.AI_V4MAPPED | socket.AI_CANONNAME,)
except socket.gaierror as e:
print('Name service failure:', e.args[1])
sys.exit(1)

info = infolist[0]
print(infolist)
socket_args = info[0:3]
address = info[4]
s = socket.socket(*socket_args)
try:
s.connect(address)
except socket.error as e:
print('Network failure:', e.args[1])
else:
print('Success: host', info[3], 'is listening on port 80')

This is the output of the previous script, where we can see that the varnish.packtpub.com service is listening on port 80:

[(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 0, 'varnish.packtpub.com', ('83.166.169.231', 80))]          Success: host varnish.packtpub.com is listening on port 80

We can use the socket.gethostbyname(hostname) method to convert a domain name into the IPv4 address format. This method is equivalent to the nslookup command we can find in many operating systems:

>> import socket
> socket.gethostbyname('packtpub.com')
'83.166.169.231'
>> socket.gethostbyname('google.com')
'216.58.210.142'

The following example will use this method to obtain an IP address from a domain. You can find the following code in the socket_remote_info.py file:

#!/usr/bin/env python3

import socket

def get_remote_info():
remote_host = 'www.packtpub.com'
try:
print ("IP address of %s: %s" %(remote_host, socket.gethostbyname(remote_host)))
except socket.error as err_msg:
print ("%s: %s" %(remote_host, err_msg))

if __name__ == '__main__':
get_remote_info()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset