Extracting IP from request in Python - python

I have a Pythonic HTTP server that is supposed to determine client's IP. How do I do that in Python? Is there any way to get the request headers and extract it from there?
PS: I'm using WebPy.

Use web.ctx:
class example:
def GET(self):
print web.ctx.ip
More info here

web.env.get('REMOTE_ADDR')

Related

How can I see the domain name or ip of the PC accessing my API method? [duplicate]

This question already has answers here:
FastAPI (starlette) get client real IP
(11 answers)
Closed 1 year ago.
I have an API method deploy on my local server,
#app.post("/test/api")
async def method():
if incoming.request.url or domain == "this":
do some operation
else:
skip it
.....
return something
Now, Few people are using my API method, but is there any way I could track who is calling my api method and do specific extra operations to the once I specified who is calling my api.
How can I track the incoming domain name or ip or url of the people who are using my api method?
Note: Need a basic example on how to acheive it if you familiar with it
Is it something possible?
If you were using flask it could happen simply by getting remote_addr as below:
from flask import FLASK, request
#app.route('/test/api', methods=['POST']):
def method():
visitor_ip = request.remote_addr
In fastapi it should happen like this: request.client.host
any way you can work with headers in your code and get many details of your client
If you can access incoming request headers, then check if X-Forwarded-For has IP address of client. If not it is possible that changing configuration of your setup will make it works as intended, however I have not experience with neither fastapi or uvicorn, so I am unable to write anything more precise.

Request a url using IP address of specific country in python

I am trying to implement a script which tests a tracking URL is reachable to the app on the app store or not.
For this, I need to get the IP address of country in question and using that IP I need to request the tracking URL. So it will be like request went through that country and we will come to know if URL is reachable to the app store for that country or not.
Question: How do I request the URL as if its requested from IP I provide?
Example:
def check_url_is_valid(url, ip_address):
# Trying to request url using ip_address
# return True or False
PS: Not expecting to complete this code but any direction or guidance is appreciated.
There is no way to get a "country's IP address" since there is no such thing. There are ranges of IP addresses corresponding to different ISP's in different locations. You should pass each request through a proxy that you know is located where you want it to be.
For that you will need to create your own proxy list for each country and pass your requests through proxy every time for each country. You should explore some possible free or payed proxies for that.
Anyway, once you do, sending a request through a proxy can be done like this:
proxyDict = {
"http" : "http://1.1.1.1:123",
"https" : "https://1.1.1.1:456",
"ftp" : "ftp://1.1.1.1:789"
}
r = requests.get(url, proxies=proxyDict)
Of course you should replace the fake addresses above with real proxies that are good for what you want.
By the way, i'm sure there are off the shelf solutions for that, so maybe you should seek them out first instead of "reinventing the wheel". For example: https://www.uptrends.com/tools/uptime
You can use web proxies that allow hotlinking or APIs, or you can use proxychains if you are on linux, or if you want to go for manual effort go for VPNs.
You need to use 3rd party service which manually checks the URL by country/region, e.g. asm.ca.com I guess there's no way you can do it for specific IP. So you should determine the country by IP first.

How to know using Django if server is secure (uses https)

I work on a Django based app, and I want to know if there's a way to know if my server uses http connections or https.
I know that using
import socket
if socket.gethostname().startswith('****'):
I can get the hostname, is it possible to do something like that so I can get to know if the hosting uses a ssl certificate?
PD: I'm a rookie here, so I'm asking to see if it's possible and, if it is, how should I do it.
Thanks
it's completely possible:
def some_request_function(request):
if request.is_secure():
#You are safe!
else:
#You are NOT safe!
More details:
https://docs.djangoproject.com/en/2.0/ref/request-response/#django.http.HttpRequest.is_secure
There simply is an is_secure() method on the request object, returning True if the connection is secure.
Depending on your specific server configuration you may also need to set SECURE_PROXY_SSL_HEADER in your settings.
django requests (HttpRequest) have is_secure method:
https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.is_secure

Get response from an ip address in python

Given an ip, how can i make an http request to this ip in python?
For example, if i want to get a file names 't1.txt' from the server that resides on '8.8.8.8', how can i do so?
I've tried using httplib and urllib2.
(It's better if the proper way will be by using standard Python libs).
Thanks a lot,
Iko.
import urllib
urllib.urlretrieve ("http://8.8.8.8/t1.txt", "t1.txt")
For simple url retrieval, urllib is perfectly OK (and is a standard Python library)...
... but if you are looking for something more easy to use in more complex cases, you should take a look at request:
import requests
r = requests.get('http://8.8.8.8/t1.txt')

How to catch request information in the python twisted xmlrpc_method?

I've study the twisted xmlrpc tutorial: http://twistedmatrix.com/documents/current/web/howto/xmlrpc.html to setup a xmlrpc server for building my xmlrpc method, but I want to know some request information from client like ip address. Tutorials just tell my add xmlrpc_ as prefix makes my method to be a remote procedure call. Does anyone know how to rewrite it? Any help will be appreciated. Or maybe I need to force client to send its ip as parameters?
def xmlrpc_some_method(self):
if request.ip in bad_ips():
return '404'
else:
return do_something()
I haven't used xmlrpc myself but by going through the source [1], it looks like you can decorate the function with t.w.x.withRequest [2] and then you will get request as the first argument to the function.
[1] http://twistedmatrix.com/trac/browser/tags/releases/twisted-13.0.0/twisted/web/xmlrpc.py#L169
[2] http://twistedmatrix.com/trac/browser/tags/releases/twisted-13.0.0/twisted/web/xmlrpc.py#L37

Categories