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

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

Related

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.

Python HTTP HEAD request keepalive

Using Python httplib or httpclient, what code do I need to use in my HTTP client to:
use an HTTP HEAD request and
contact a web server by just specifying only its IP address and
contact a web server without specifying any webpage (or homepage) on the request
to extend its HTTP connection using Keepalive messages?
I used the following code example but it has two problems:
It does not extend the http connection using Keepalive,
It gives me an error message "500 Domain Not Found" if I use the IP address instead of the domain name.
import http.client
Connection = http.client.HTTPConnection("www.python.org")
Connection.request("HEAD", "")
response = Connection.getresponse()
print(response.status, response.reason)
requests allows to:
send requests with HEAD method:
import requests
resp = requests.head("http://www.python.org")
use sessions for auto Keep-alive: info
s = requests.Session()
resp = s.head("http://www.python.org")
resp2 = s.get("http://www.python.org/")
Regarding using the IP address instead of domain, that has nothing to do with your request. Most sites use some kind of virtual hosts, so they don't respond to IP address only to specific domain names. If you ask for the IP address you may get a 500 error or a message error.

Send mail with Chilkat Mailman using authenticated HTTP proxy

I am trying to send emails using the chilkat mailman api in python, through an authenticated http proxy. I have followed the instructions from the Chilkat docs to the best of my ability but am having issues with the proxy server. I have verified that this proxy works given the specified port and auth using phantomjs scripts.
import chilkat
# The mailman object is used for sending and receiving email.
mailman = chilkat.CkMailMan()
# set the http proxy
mailman.put_HttpProxyAuthMethod("LOGIN")
mailman.put_HttpProxyHostname("xxx.xxx.xxx.xxx")
mailman.put_HttpProxyPort(xxxxx)
mailman.put_HttpProxyUsername("xxxxx")
mailman.put_HttpProxyPassword("xxxxx")
# Set the SMTP server.
mailman.put_SmtpHost("smtp.live.com")
mailman.put_StartTLS(True)
mailman.put_SmtpPort(25)
# Set the SMTP login/password (if required)
mailman.put_SmtpUsername("xxxxxxx")
mailman.put_SmtpPassword("xxxxxxx")
# Create a new email object
email = chilkat.CkEmail()
email.put_Subject("This is a test")
email.put_Body("This is a test")
email.put_From("name#email.com")
email.AddTo("Chris Johnson","name#email.com")
# Call SendEmail to connect to the SMTP server via the HTTP proxy and send.
success = mailman.SendEmail(email)
if (success != True):
print(mailman.lastErrorText())
sys.exit()
If I take out the section that sets the proxy, the mail is successfully sent. Is there some other attribute I am missing?
Though Chillkat's mailman has support for HTTP proxies, it seems that it doesn't support pure HTTP proxies. Mainly, the proxy that you use must also support other protocols because SMTP doesn't broadcast over HTTP. As my proxy only supported HTTP protocol, it wouldn't work for me. I swapped to use a SOCKS proxy and now everything is working fine.

about get client ip address in python CGI

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?

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