How to specify source IP address in python requests [duplicate] - python

This question already has answers here:
Requests, bind to an ip
(3 answers)
Closed 8 months ago.
I have several IP addresses configured on my network interface card. I need to specify through which IP address python requests must connect to the web server. Is it possible?
import requests
r = requests.get("http://example.com/foo/bar")
I need to force requests.get to perform http request through a dasignated IP address.
Is there any other http library that support this feature in python?

Are you running a Django/Django-Rest project? Because, if so, you can just specify a custom host and port into the python manage.py runserver command! Like so:
python manage.py runserver 0.0.0.0:8001
Hope I understood the problem

You can use a SourceAddressAdapter from requests-toolbelt:
import requests
from requests_toolbelt.adapters import source
source = source.SourceAddressAdapter('127.0.0.1')
with requests.Session() as session:
session.mount('http://', source)
r = session.get("http://example.com/foo/bar")

Related

Proxy detection in Python3

The script below works fine when I am using script at home (same PC!):
import urllib.request
x = urllib.request.urlopen('https://www.google.com/')
print(x.read())
the same does not work using the same script when I am connected at work. I do not know proxy address or IP, so my script should use the same way as IE or anything else on this PC.
I found some suggestions about using proxy , but the point it I do not know proxy IP or details. When I move the script to another PC it might have different proxy, so I think hardcoding it is not good approach.
Can I somehow inform Python to autodetect proxy settings?
Going by your eample, I am assuming you are doing a https call over proxy. The urllib documentation hints its not supported. So, instead you may have to settle down with http.
In order to validate that there is nothing wrong with your setup, you may try to do open the IP directly:
import urllib
# IP address for `http://www.google.com` is `216.58.205.196`
x = urllib.urlopen('http://216.58.205.196')
print x.read()
A. There are lots of complaints about Python's trippy auto-detect proxy settings in various other threads. I had this issue only once years ago and I opted for setting a fixed proxy instead of trying to configure auto-detect. To know your proxy, you can go to chrome url chrome://net-internals/#proxy or run netstat -an | grep EST command.
B. Once you have proxy address, you can use following code:
import urllib
# IP address for `http://www.google.com` is `216.58.205.196`
x = urllib.urlopen('http://216.58.205.196',
proxies={'http': 'http://www.someproxy.com:3128'})
print x.read()
If you cannot avoid https, then you may consider requests library. I didn't test this, but requests documentation looks quite promising. This is how it can be done!
import requests
proxies = {
'http': 'http://10.10.1.10:3128',
'https': 'http://10.10.1.10:1080',
}
requests.get('https://example.org', proxies=proxies)
Edit:
1: You may need to setup proxy authentication in order for 3.B. to work
2: For Special characters, you would need to have the password in unicode: 'p#ssw0rd'.decode('utf-8')
Hope this helps!

Python: Retrieve the Mac Address of the client using bottle framework [duplicate]

This question already has answers here:
How to get Client Machine's Mac Address in a Web application
(3 answers)
Closed 7 years ago.
I am looking to retrieve the Mac_Address of the user accessing my application.
I have tried the following code:
import uuid
from uuid import getnode as get_mac
mac_address = get_mac()
mac_address = ':'.join(("%012X" % mac_address)[i:i+2] for i in range(0, 12, 2))
This works OK but returns the mac address of the server and not the client. Is there a way to retrieve the mac address of the client?
The HTTP protocol does not send the MAC address of the client to the server, so there's no way for your server-side application to access it.
The client's MAC address is never revealed to the outside network. Thus it is not possible to get it. The only way for it is to install your app on user's machine.
You will only be able to retrieve the MAC address of users who happen to be on the same LAN network segment as you are, so it is probably not a good idea to bake in the assumption that you can rely on the user's MAC address being available.

python equivalent for curl --interface

What is the python equivalent for following shell command:
curl --interface 10.91.56.2 http:/10.91.55.3/file0.txt
????
I am using CentOS6.5-Linux and I want to send http request from virtual IP addresses like eth0:0,eth0:1,eth0:2,etc simultaneously with eth0. I am actually trying to make one traffic generator tool using python. I have been successful in sending multiple and concurrent http requests and now my next step is to send such requests from multiple ip addresses.I used following cURL command to send request from eth0:1 " curl--interface 10.91.56.2 http:/10.91.55.3/file0.txt" and I was successful in generating traffic from virtual eth0:1. Can anyone guide me how to do this using python? 10.91.56.2 is my virtual eth0:1 IP interface and 10.91.55.3 is my server address... –
Python Urllib2 provides perfect platform for making any HTTP request. In your case you can use urlopen() function...
More about this libary can be found in the below link:
how-to-use-urllib2-in-python
For me, eth0's ip is 10.91.56.3 and eth0:1's ip is 10.91.56.4 so, to generate traffic using 10.91.56.4(eth0:1)
Followed answer by #AKX here
In above answer in 3rd class write your interface's ip instead of 127.0.0.1 eg in my case i did like this:
class BindableHTTPHandler(urllib2.HTTPHandler):
def http_open(self, req):
return self.do_open(BindableHTTPConnectionFactory('10.91.56.4'), req)

HTTP requests using multiple IP addresses on python [duplicate]

This question already has an answer here:
HTTP Requests using a range of IP address on python
(1 answer)
Closed 8 years ago.
I'm writing a python script that will send http requests concurrently to the urls mentioned in a file using python. The script works fine for a single IP address. The OS I'm using is linux. I've generated virtual IP addresses like eth0:1,eth0:2 etc. I want to send HTTP requests using these virtual IP addresses along with the eth0 IP address concurrently. I use the requests module for http requests and threading module for concurrent requests. Kindly help me. I'm trying to develop a web testing tool.
I think you wanted to avoid "Crawl Delay" and do faster crawl on one server!
In this case, Remote Web Server will recognize request from only one IP!!
I think using parallel + curl + python script is more simple and best way.
or use https://pypi.python.org/pypi/pyparallelcurl/0.0.4
or use a lot of servers.
and refer to https://code.google.com/p/httplib2/issues/detail?id=91

Flask:Python - How to check IP address of the request [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get ip address of visitors using Python (specifically Flask micro-framework)
I'm creating REST api with flask. I want to allow requests only from one (or more later on) IP addresses. How do I check for IP address in my view?
Get the headers from Incoming Request Data and find the REMOTE_ADDR
if you are using google app engine you would use self.request.remote_addr

Categories