I have the following code:
f = urllib2.urlopen(url) data = f.read() f.close()
Works on a machine with two network interfaces. I would like to indicate which interface I need to use the code. In particular, I want him to use only the one that he uses by default ... but I can figure out what is, if I can just select the interface.
What is the easiest / best / most pythonic way to do this?
Not a complete solution yet, but if you use only simple socket objects, you can do what you need:
import socket s = socket.socket() s.bind(("127.0.0.1", 0)) # replace "127.0.0.1" by the local IP of the interface to use s.connect(("remote_server.com", 80))
Thus, you force the system to bind the socket to the desired network interface.
If you use Twisted twisted.web.client.Agent , you can achieve this with:
twisted.web.client.Agent
from twisted.internet import reactor from twisted.web.client import Agent agent = Agent(reactor, bindAddress=("10.0.0.1", 0))
And then using agent usual way.
agent
Source: https://habr.com/ru/post/902224/More articles:jQuery - Why is creating an object with a class per line ($ ('')) slower than creating the same object and executing the addClass () method? - jqueryextended gif library - c #https://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/902221/python-decorate-all-methods-of-subclass-and-provide-means-to-override&usg=ALkJrhibACYxGqccR_EtoGeViivC84MnEAAndroid button textAppearance - androidJquerymobile download message subject - jqueryEditorFor does not work on derived type - vb.netChrome -webkit-play animation - cssAccess to F # from within C # Code - listWhat is the purpose of the SO_SNDLOWAT socket - linuxNew iOS5 UISwitch doesn't seem to be disabled in UITableViewCell - iosAll Articles