我有两个可以同时访问互联网的网络接口(WiFi和以太网)。假设我的界面是eth(以太网)和wlp2(wifi)。我需要特定的请求才能通过eth接口,其他需要通过wpl2。
就像是:
// Through "eth"
request.post(url="http://myapi.com/store_ip", iface="eth")
// Through "wlp2"
request.post(url="http://myapi.com/log", iface="wlp2")
Run Code Online (Sandbox Code Playgroud)
我正在使用requests,但是我可以使用pycurl或urllib如果没有任何方法可以使用它requests。
如何在python请求模块中指定源接口?指的是请求,绑定到一个IP,它不起作用。
我正在尝试学习一些python,我在测试中遇到了逻辑问题.目前我的代码是以一种方式编写的,即当进程启动时,对source_address的绑定不会改变
import socket
import requests
real_create_conn = socket.create_connection
def set_src_addr(*args):
address, timeout = args[0], args[1]
source_address = ('201.X.X.1', 0)
return real_create_conn(address, timeout, source_address)
socket.create_connection = set_src_addr
r = requests.get('http://www.mywebpage.com/main')
print r.status_code
if r.status_code == 404
print "Webpage Down!"
r = requests.get('http://www.mywebpage.com/blog')
print r.status_code
if r.status_code == 204
print "Error occured!"
Run Code Online (Sandbox Code Playgroud)
我正在寻找这样的事情
import socket
import requests
While 1:
#bind to source address 201.X.X.1
#Send request to main webpage
#print result
time.sleep(300) # 5 minutes
#bind to source address 201.X.X.12
#Send request …Run Code Online (Sandbox Code Playgroud) 这可能是一件非常简单的事情。我是python的新手,所以不要钉死我。
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, IN.SO_BINDTODEVICE, "eth1"+'\0')
Run Code Online (Sandbox Code Playgroud)
上面的命令给了我:
NameError: name 'IN' is not defined
Run Code Online (Sandbox Code Playgroud)
我唯一的进口是
import socket
Run Code Online (Sandbox Code Playgroud)