Avi Shabat
Avi Shabat

Reputation: 69

getting the host name of the connecting user with socket

I need help with getting the hostname of a client that's connecting with a socket

partial code:

s = socket.socket()
host = ''
port = 5007
s.bind((host, port))
s.listen(100)
conn, addr = s.accept()
print('connected by: ', addr)

I want that instead of printing the ip address of the user it will print his hostname.

Thanks in advance.

Upvotes: 0

Views: 191

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123551

If there is a DNS PTR record for the clients IP address then gethostbyaddr will give you the name which is setup with this record. Note that this call will block until the DNS lookup is done (or failed), that there is no PTR record for all IP addresses and that you cannot really trust the contents of this record since the owner of the IP address can claim any hostname, even if the domain is owned by somebody else.

Upvotes: 1

Related Questions