473,508 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Controlling source IP address within urllib2

Dan
Does anybody know how to control the source IP address (IPv4) when
using the urllib2 library? I have a Linux box with several IP
addresses in the same subnet, and I want to simulate several
individuals within that subnet accessing web pages independently. I
need the functionality of urllib2 because there will be redirects and
other HTTP-type functions to implement. It would be nice if I could
create (and bind) sockets myself and then tell the urllib functions to
use those sockets. Perhaps there is some sort of "back-door" way of
doing this??? Any hints are appreciated!
-Dan

Jul 19 '05 #1
4 3722
Dan
John,
Thanks for your input. I can kind of see the light in this, but I'm
having difficulty knowing where the "do_open" method comes from. Also,
I'll need to follow redirects, so I assume then I would add a
HTTPRedirectHandler instance to the urllib2.build_opener. (?) Thanks
again for your help.
-Dan

Jul 19 '05 #2
"Dan" <to****@hotmail.com> writes:
John,
Thanks for your input. I can kind of see the light in this, but I'm
having difficulty knowing where the "do_open" method comes from. Also,
AbstractHTTPHandler

I'll need to follow redirects, so I assume then I would add a
HTTPRedirectHandler instance to the urllib2.build_opener. (?) Thanks


No. It does that by default.
John
Jul 19 '05 #3
Dan
John,
Thanks again for your help!
I think that the do_open function in AbstractHTTPHandler does not
return the correct object type as required by the opener. When I
include the code you recommended, the implementation comes back with
the message, "urlopen error unknown url type: http". Strange, because
I would think that overriding the "http_open" function in the handler
would have signaled that this function is capable of handling http. If
I call the HTTPHandler base class "http_open" function from within the
derived class, all works okay, but of course, I don't get to use the
source IP address I wanted to use.
I'll keep trying and let you know what I find.
-Dan

Jul 19 '05 #4
Dan
Just FYI ... I finally got this to work. Unfortunately, I was unable
to use the urllib2 library. The embedded Linux team here informed me
that the urllib2 library will not be available on the machine I wanted
to deploy on, so I had to go back to using the urllib and httplib
libraries. After trying several things, I finally gave up and hacked
the library code directly. It turned out to be quite easy. I just had
to wedge in a "self.sock.bind()" call in front of the
"self.sock.connect()" call within the HTTPConnection class inside the
httplib library. (I used the bind() function to nail down the source
IP address of the client.) Of course, I had to weave in some code that
allowed me to pass the client IP address through the URLopener class in
the urllib library. Everything seems to work so far.

John, thanks again for your help. You pointed me in the right
direction.

-Dan

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6072
by: John F Dutcher | last post by:
Can anyone comment on why the code shown in the Python error is in some way incorrect...or is there a problem with Python on my hoster's site ?? The highlites don't seem to show here...but line...
5
7378
by: Pascal | last post by:
Hello, I want to acces my OWA (Outlook Web Acces - http Exchange interface) server with urllib2 but, when I try, I've always a 401 http error. Can someone help me (and us)? Thanks. ...
3
1455
by: rock69 | last post by:
Hi all :) I got this address: http://www.infomedia.it/immagini/riviste/covers/cp/cp137.jpg and I would like to download that image and save it to a local file. How do you do that in Python?
1
3350
by: Ray Slakinski | last post by:
Hello, I have defined a function to set an opener for urllib2, this opener defines any proxy and http authentication that is required. If the proxy has authencation itself and requests an...
1
2370
by: joemynz | last post by:
Help please with a URLError. Invoking a url that works in Firefox and IE results in a "urlerror 7, no address ..." in python. I need to debug why. Traceback is below. There's a redirect when the...
1
5734
by: Alessandro Fachin | last post by:
I write this simply code that should give me the access to private page with htaccess using a proxy, i don't known because it's wrong... import urllib,urllib2 #input url...
5
3746
by: cjl | last post by:
Hi. I am trying to screen scrape some stock data from yahoo, so I am trying to use urllib2 to retrieve the html and beautiful soup for the parsing. Maybe (most likely) I am doing something...
5
1706
by: pyapplico | last post by:
Is there any possible way that I can place a .py file on the internet, and use that source code in an .py file on my computer?
5
2045
by: Timothy Grant | last post by:
On Tue, Aug 5, 2008 at 2:50 PM, David York <davideyork@gmail.comwrote: I'm not sure what you are trying to accomplish. The machine I'm typing this on has a 192.168.x.x number. The router that...
0
7226
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
422
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.