Search code examples
pythonipprotocolsports

how to get protocol from ip and port with Python3?


i need get protocol(HTTP, FTP, SSMTP, IMAP, ETC) from ip address and port with Python3.

Example:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
protocol = s.connect( ("localhost", 80 )
print protocol #print HTTP

Solution

  • In general, it's not possible to detect the protocol type of a server listening on some port. The best you can get is request the name of the service that is normally associated with some port number:

    >>> socket.getservbyport(80)
    'www'