Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - How can I get just the IP from socket.getpeername()?

Tags:

python

sockets

I am curious to know how you can return ONLY the IP address of the connected user using

ip = socket.getpeername()

Because whenever I use that in my server it returns the IP and socket port, I only want the IP, or at least a way to replace the port with Null... I tried using the replace function but that just gives me an error

attributeError: 'tuple' object has no attribute 'replace

1 Answers

As the error suggests, getpeername returns a tuple. To get the IP you should simply take the first element:

ip = socket.getpeername()[0]
like image 159
DeepSpace Avatar answered Oct 24 '25 21:10

DeepSpace



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!