Python Forum
Configuring requests module to use secondary IP on server for outbound requests
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Configuring requests module to use secondary IP on server for outbound requests
#1
Hello all.
I have a server which has two IP addresses - primary and secondary.

I have written a python script on server which keeps calling a remote service as HTTP request through "requests" module. By default it uses primary IP of server. 

My question is, is there a way to make script use other IP of server for all outbound requests randomly?


Server uses OpenVZ for virtual infrastructure and I can change ip for outbound requests manually via SolusVM control panel but Can I configure script such that it starts using other ip for outbound requests dynamically?

If not, is it possible to setup a proxy server (script running as daemon) on this server which uses this other IP for outbound requests and then I can use this service running on a port as "proxy" in my requests module call?
Reply
#2
Because of the way sockets are made in python, you have to kind of do it yourself.  From StackOverflow: 

http://stackoverflow.com/a/1150423 Wrote:Unfortunately the stack of standard library modules in use (urllib2, httplib, socket) is somewhat badly designed for the purpose -- at the key point in the operation, HTTPConnection.connect (in httplib) delegates to socket.create_connection, which in turn gives you no "hook" whatsoever between the creation of the socket instance sock and the sock.connect call, for you to insert the sock.bind just before sock.connect that is what you need to set the source IP (I'm evangelizing widely for NOT designing abstractions in such an airtight, excessively-encapsulated way -- I'll be speaking about that at OSCON this Thursday under the title "Zen and the Art of Abstraction Maintenance" -- but here your problem is how to deal with a stack of abstractions that WERE designed this way, sigh).

When you're facing such problems you only have two not-so-good solutions: either copy, paste and edit the misdesigned code into which you need to place a "hook" that the original designer didn't cater for; or, "monkey-patch" that code. Neither is GOOD, but both can work, so at least let's be thankful that we have such options (by using an open-source and dynamic language). In this case, I think I'd go for monkey-patching (which is bad, but copy and paste coding is even worse) -- a code fragment such as:

import socket
true_socket = socket.socket
def bound_socket(*a, **k):
    sock = true_socket(*a, **k)
    sock.bind((sourceIP, 0))
    return sock
socket.socket = bound_socket
Depending on your exact needs (do you need all sockets to be bound to the same source IP, or...?) you could simply run this before using urllib2 normally, or (in more complex ways of course) run it at need just for those outgoing sockets you DO need to bind in a certain way (then each time restore socket.socket = true_socket to get out of the way for future sockets yet to be created). The second alternative adds its own complications to orchestrate properly, so I'm waiting for you to clarify whether you do need such complications before explaining them all.

AKX's good answer is a variant on the "copy / paste / edit" alternative so I don't need to expand much on that -- note however that it doesn't exactly reproduce socket.create_connection in its connect method, see the source here (at the very end of the page) and decide what other functionality of the create_connection function you may want to embody in your copied/pasted/edited version if you decide to go that route.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko Server -- Exception (server): Error reading SSH protocol banner ujlain 3 4,497 Jul-24-2023, 06:52 AM
Last Post: Gribouillis
  asyncio stops responding to requests mansky 1 2,772 Aug-30-2021, 10:08 PM
Last Post: mansky
  WSAStartup error with requests yllawwally 1 2,999 Feb-23-2021, 01:53 PM
Last Post: yllawwally
  Http connection error when using API request (requests library) zazas321 1 3,078 Oct-13-2020, 05:58 AM
Last Post: zazas321
  Test http requests JohnnyCoffee 2 2,374 Jul-10-2020, 03:14 AM
Last Post: JohnnyCoffee
  how to send an image from server to client using PICKLE module dafdaf 1 3,104 Jun-02-2020, 01:08 PM
Last Post: nuffink
  sending requests over TCP to terminal Oolongtea 5 2,514 Aug-31-2019, 10:25 AM
Last Post: wavic
  UDP to TCP requests script batchenr 0 2,567 Jun-30-2019, 07:52 AM
Last Post: batchenr
  Send data BMP180 between client and server trought module socket smalhao 0 2,833 Jul-30-2018, 12:56 PM
Last Post: smalhao
  Error getting HTTP 200 response with requests.get while wget works fine sonicblind 0 4,991 Oct-18-2017, 02:24 PM
Last Post: sonicblind

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020