Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get viewers IP address in python

When a user opens my page, a PHP client is called on page load. The client calls to python gearman worker.

In that python worker script I wanted to get users IP address. I tried both

ip = ([(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close())
    for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1])

and

urlopen('http://ip.42.pl/raw').read()

But this gives my server IP, rather than the user's IP.

like image 560
nlper Avatar asked Feb 15 '26 06:02

nlper


1 Answers

You cannot get the IP of the user from the Python script. You can either get it from PHP, or pass it from your PHP script to your Python script when you call your job.

like image 183
Alex Palcuie Avatar answered Feb 17 '26 20:02

Alex Palcuie



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!