How can I get the port number of a newly accepted connection in python?

When I create a socket on the server and accept the incoming connection:

conn, addr = s.accept()

Both print conn.getsockname()and print s.getsockname()print the same port number.

I thought that "conn" should have represented a NEW socket. How to get the port number of this new socket?

Thank!

+3
source share
2 answers

The local port remains the same. You need a remote side port. You can use getpeernamefor this (or the second element of the return value accept).

+5
source

This is a new socket, but it has the same local port as the original socket for listening.

0
source

Source: https://habr.com/ru/post/1774588/


All Articles