Send http request through specific network interface

后端 未结 3 1454
春和景丽
春和景丽 2020-12-07 01:46

I have two network interfaces (wifi and ethernet) both with internet access. Let\'s say my interfaces are eth (ethernet) and wlp2 (wifi). I need sp

3条回答
  •  一生所求
    2020-12-07 02:09

    Try changing the internal IP (192.168.0.200) to the corresponding iface in the code below.

    import requests
    from requests_toolbelt.adapters import source
    
    def check_ip(inet_addr):
        s = requests.Session()
        iface = source.SourceAddressAdapter(inet_addr)
        s.mount('http://', iface)
        s.mount('https://', iface)
        url = 'https://emapp.cc/get_my_ip'
        resp = s.get(url)
        print(resp.text)
    
    if __name__ == '__main__':
        check_ip('192.168.0.200')
    

提交回复
热议问题