Olaf Erlandsen
Olaf Erlandsen

Reputation: 6036

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

Upvotes: 1

Views: 2554

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 601599

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'

Upvotes: 5

Related Questions