0

I have a few IP addresses and I need to 'bind' a request to one or another one of them dynamically. I am using python requests library to make http requests (GET or POST). Is it possible to bind my requests to needed IP?

PS: I asked because it is possible in .net C# to bind socket to needed output IP (IPEndPoint) so I thought it may be possible in python requests too.

raj454raj
  • 299
  • 3
  • 12
Alexey Kulikov
  • 1,097
  • 1
  • 14
  • 38
  • Could you provide a minimum working example of what you've got so far and tell us what you've tried to get what you want? – Aleksander Lidtke Jun 30 '15 at 21:09
  • http://stackoverflow.com/questions/28773033/python-requests-how-to-bind-to-different-source-ip-for-each-request – kichik Jun 30 '15 at 21:24

1 Answers1

-1

What you are looking for is the socket library. Here is an example to get you started.

import socket

HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 50007              # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1024)
    if not data: break
    conn.sendall(data)
conn.close()

Take a look at https://docs.python.org/2/library/socket.html, and toward the bottom you will see some examples for how to use the library. Version 3 documentation is here - https://docs.python.org/3/library/socket.html.

Tim
  • 69
  • 5