So as the title suggests is there any way that I can get the port of a website/ip with a socket connection? I assigned socket.gethostbyname()
to the simple variable "ip" so that I can print out the result in console but can I do the same with the port? Here is the website that I am using to learn with > https://docs.python.org/3/library/socket.html < and what I am wondering is if this is a good source or does anyone know a better one? Thank you much :D
You can get the currently used port with getsockname
:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('0.0.0.0', 0))
print('listening on port:', sock.getsockname()[1])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With