about get client ip address in python CGI - python

I am trying to get the original client ip address in a python cgi script. The client connect to the web server with a proxy. Following code always returns the proxy ip address. I tested all the env variable, HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR all return None. Is there any other way to get the client ip behind a proxy? like can I read the http header in a python cgi?
ipaddr = (getenv("HTTP_CLIENT_IP") or
getenv("HTTP_X_FORWARDED_FOR") or
getenv("REMOTE_ADDR") or
"UNKNOWN")

Do you want to:
find the client's IP address in the headers
use some sort of javascript to tell you?
For the former, you need the proxy to give you the client address.
Have you tried cgi.print_environ_usage() and looking for the IP address?

Related

How to get the ip address of a request machine using python (On Sanic)

I am trying to get the IP address of the requesting computer on my server. I can successfully get the IP-address from request header if the request came from a web browser. The code example below. However, I cannot fetch the client IP, if I send the request via curl/postman. I checked the nginx log, and I found there is a log of my public IP when I sent a curl request. How can I achieve this?
PS: I am using the Sanic Framework.
client_ip = request.headers.get('x-real-ip')
In sanic's docs for Request Data:
ip (str) - IP address of the requester.
Therefore, just use
client_ip = request.ip

Listening and capturing multiple response for a dns query in python

I am trying to send a dns query to an specific dns resolver in python.
Here is my code to set my resolver and sending my dns query to find the ip address of my domain name:
import dns.resolver #import the module
myResolver = dns.resolver.Resolver()
myResolver.nameservers = ['my resolver ip address']
myResolver.port = 5300#port number of resolver listening to dns queries
myAnswers = myResolver.query("google.com")
print myAnswers
This code works very well when I receive just one response from the resolver. The response can has multiple ip addresses and that is ok too. The question is that, my dns resolver has been configured to send multiple response to a A dns query and I need to receive first, second and third one and check them and if one of them passed my checks, print the response in the console.
My problem is that I do not know how can I get multiple responses with myAnswers = myResolver.query("google.com") command. Because this command return the response and save that in myAnswer. I know I should use multithreading and listen to incoming responses from my dns resolver. But I do not know how I can run my thread with this command,myAnswers = myResolver.query("mytargetdomain.com",'a')?
Thanks.

How to pass correct client ip from nginx running in one container to python-flask app running 2nd container? [duplicate]

Heroku proxies requests from a client to server, so you have to parse the X-Forwarded-For to find the originating IP address.
The general format of the X-Forwarded-For is:
X-Forwarded-For: client1, proxy1, proxy2
Using werkzeug on flask, I'm trying to come up with a solution in order to access the originating IP of the client.
Does anyone know a good way to do this?
Thank you!
Werkzeug (and Flask) store headers in an instance of werkzeug.datastructures.Headers. You should be able to do something like this:
provided_ips = request.headers.getlist("X-Forwarded-For")
# The first entry in the list should be the client's IP.
Alternately, you could use request.access_route (thanks #Bastian for pointing that out!):
provided_ips = request.access_route
# First entry in the list is the client's IP
This is what I use in Django. See this https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.get_host
Note: At least on Heroku HTTP_X_FORWARDED_FOR will be an array of IP addresses. The first one is the client IP the rest are proxy server IPs.
in settings.py:
USE_X_FORWARDED_HOST = True
in your views.py:
if 'HTTP_X_FORWARDED_FOR' in request.META:
ip_adds = request.META['HTTP_X_FORWARDED_FOR'].split(",")
ip = ip_adds[0]
else:
ip = request.META['REMOTE_ADDR']

login into local FTP server with ip

I am trying to log into a FTP server that I am only running locally in my network. To do this I have to use my ip address to use as the server address (see code below). However each time I get a gaierror: [Errno 11004] getaddrinfo failed error.
Can anyone look at my code and see if I am making any error that may be the cause of this address issue? Also I can log into the ftp server fine from my browser so I know the server is up and running correctly, also anonymous login to the server is allowed .
#import the ftp lib.
from ftplib import FTP
#enter the address of the ftp server to use, use ip address since server is ran locally
ftp = FTP('ftp://192.168.1.130')
#logs into the ftp server
ftp.login()
You're including the protocol with the hostname which is incorrect. What's happening is the library is trying to resolve "ftp://192.168.1.130" (instead of "192.168.1.130"), which isn't a valid address.
#Wrong
ftp = FTP('ftp://192.168.1.130')
#Right
ftp = FTP('192.168.1.130')
http://docs.python.org/library/ftplib.html
According to ftplib documentation, you shoud just give him the host address/IP, and not a string representing an URL. So here, you should simply do:
ftp = FTP('192.168.1.130')

In need of a python script to upload a file with IP address every few minutes

I'm using windows server 2008, and one of the things I need to do in order to pair to a domain name is send a file with the computers current IP address (it's not static) to a server via sftp every few minutes. The problem is that I'm not sure how to do this.
I would send it via XMPP. You can set up a listener service for the server.
Send an xmpp message using a python library
Here are some ideas on XMPP servers to run on your IIS server (listening to recieve the incoming messages from clients http://metajack.im/2008/08/26/choosing-an-xmpp-server/
Pretzel looks nice
this python code can be run client side to get the public IP address.
host, aliaslist, lan_ip = socket.gethostbyname_ex(socket.gethostname())
print host
print aliaslist
print lan_ip[0]
Than you would send via XMPP message containing the IP to the server you have set up on your IIS server. Depending on what you want to do with the IP address once it gets to the server, you will handle the message serverside

Categories

Resources