I have a server with access to 9 different IP addresses that I want to use for different purposes and I wish to keep them separated. I am going to be doing my work using UrlLib2, is there a way I can specify which IP address I am using through Python?

I am not sure if I am being completely clear, let me try and give an example. Let's say I have 9 programs that connect to the web and retrieve data each hour running automatically as a scheduled task. If I want each program to run on a separate IP address, is there a way within Python I could specify which IP each program connects to when it goes to retrieve data from the web?

Thanks,
Rob

Recommended Answers

All 3 Replies

One possibility, depending on how you run your tasks, is to pass the IP address on the command line, thusly: C:\Work>blort.py 10.3.21.109 . The IP address would show up in the program as sys.argv[1] in this case:

C:\Work>type blort.py
import sys

print sys.argv

C:\Work>blort.py 10.3.21.109
['C:\\Work\\blort.py', '10.3.21.109']

The IP address is of type string and of course you could just as easily use a node name, if it is known to your server.

If each program has a different name, you could even just check sys.argv[0] to see what named program you're running, and select the appropriate IP address for that program name.

If this doesn't make any sense to you, then I probably didn't understand what you are trying to do.

Thanks for the reply. It makes sense but it's not exactly what I was asking. I know the IP's ahead of time and can keep them hard-coded in the program. What I don't understand how to do is use that particular IP address to make a connection (as opposed to my default IP) when going to a webpage to retrieve data.

So instead of the standard URLLib2.urlopen('www.examplepage.com'), I want a way to specify which IP address is used to make the connection that goes and accesses 'examplepage.com'. Sorry, I might not be doing a great job of explaiing it, is this more clear?

If I understand you, then it's even easier.

Try the following: for x in urllib2.urlopen('http://209.85.165.99/'): print x In my part of the world, 209.85.165.99 is the IP address of google.com. Can't you just substitute your own IP address[es]?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.