log incoming ip addresses in web.py - python

I'd like to log the IP addresses of machines that access my web.py application. How can I access this data within the web.py framework? I'm happy to send the address to my own logger.

Use web.ctx['ip'] to get the remote ip address. Here is a link which explains what's in the context.

Related

Access django main page by its name not ip

is there a way to configure a Django server in that way so I can open my local webpage by typing 'weatherstation.local' and not by the IP:port?
in your router you might be able to set DNS. if DNS administration exists, then here you can set different domain names for IP addresses on your network.
You can give your ip address multiple domain names, and then you can use somthing like NGINX to route the domain calls to different services that you are running on your server.
Good luck, I hope this was helpful ;-)

Getting Client IP address while using development server in Flask

What I am doing
I have a flask website and I am making it accessible to a client using ngrok tunneling.
What I want
I am trying to get the IP address of the client.
What I have done so far
I have tried these so far,
request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
and
request.environ['REMOTE_ADDR']
But both of them are returning 127.0.0.1. I have also checked out this question But it didn't help me out since the answer written there are for getting client IP's in production server environment whereas I am looking for any method using which I can get IP address of client in the development mode of server which is tunneled using ngrok.
I have two possible methods in my mind,
If I can get the IP address of the connection requester from Ngrok. I don't know is there any way to do it but this can solve my problem.
Or I add something to my javascript code so that whenever the index page loads up it sends an ajax request to the server telling it the IP address of the client. (Correct me if wrong)
In case of Flask , you cannot get the client ip address directly on the server side but you cannot get the IP address if your web app grabs the client ip and then using AJAX request sends it back to the server so that you can log it.
That's the only possible way i think you can do it in flask.

How to get the IP address of the request to a Heroku app?

Heroku has a routing system to forward requests to the dynos. My application needs to know from where the request came, but it always gets random addresses in a network, probably Heroku's internals.
And I see that in the logs, it (Heroku's router) gets my IP address and forwards the request. Is there a way to get the actual IP address of a request?
My application is written in Python, using Flask
Checking Flask's documentation on filtering headers etc., I found that:
request.headers['X-Forwarded-For']
is where you'll get the client's real IP address.
From a deleted comment by OP, this article provides a safer solution.
You want to preserve the IP from request.remote_addr when locally or if hosting the site somewhere else:
def getIP():
if 'X-Forwarded-For' in request.headers:
return request.headers['X-Forwarded-For']
return request.remote_addr

Get an IP adress of a client of a SOAP service

I'm writing a SOAP service using python and soaplib. I need to get IP adresses of all clients of the service to store them to the log file. How can I do that?
One way to do this is to implement a "hook" which is called at different stages of the wsgi executation. See the section "Hooks" in the soaplib readme file for details and the example hook.py in that distribution.
For example, you could implement onMethodExec and then use the wsgi environ.get('REMOTE_ADDR') to obtain the client's IP address and log it.

IP address of domain on shared host

I have domain on a shared hosting provider. How do I find the direct IP address of my domain using Python?
Is it possible to post to a script on my domain using the IP address and not the website itself?
Thanks.
I guess the IP should be static so do you really need to look it up more than once?
You need to specify the domain name so that the webserver knows which host configuration to use if you don't have a dedicated IP or your host is the default for that webserver
import socket
socket.gethostbyname("www.stackoverflow.com")
'69.59.196.211'
will get you the ip address (as a string) of your domain.
However, if it's shared hosting I would think it highly unlikely that you'll be able to access your hosting via the ip - most likely you'll have something like Apache's VirtualHost Directive in place which limits you to only 'seeing' requests to your domain. Requests to the IP address will be served by some default configuration.
Would very much depend on the nature of your hosting.
A curious request ...
To look up a domain name, do something like this:
import socket
ipaddress = socket.gethostbyname('www.bbc.co.uk')
Regarding posting to the IP address:
I don't think it would work in the normal way (like from a browser), because there will probably be many sites held under that address.
But, I guess you could do it in a very manual way, using a programming language (e.g. Python), if you connected a client socket to the site's IP address, but still sent the website's name in the HTTP Host request header.
I don't know if that poses more questions than it answers, and I don't know why you'd want to do either of the above, but there it is.
Good luck!

Categories