Send http request through specific network interface

后端 未结 3 1467
春和景丽
春和景丽 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:14

    I found a way using pycurl. This works like a charm.

    import pycurl
    from io import BytesIO
    import json
    
    
    def curl_post(url, data, iface=None):
        c = pycurl.Curl()
        buffer = BytesIO()
        c.setopt(pycurl.URL, url)
        c.setopt(pycurl.POST, True)
        c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json'])
        c.setopt(pycurl.TIMEOUT, 10)
        c.setopt(pycurl.WRITEFUNCTION, buffer.write)
        c.setopt(pycurl.POSTFIELDS, data)
        if iface:
            c.setopt(pycurl.INTERFACE, iface)
        c.perform()
    
        # Json response
        resp = buffer.getvalue().decode('UTF-8')
    
        #  Check response is a JSON if not there was an error
        try:
            resp = json.loads(resp)
        except json.decoder.JSONDecodeError:
            pass
    
        buffer.close()
        c.close()
        return resp
    
    
    if __name__ == '__main__':
        dat = {"id": 52, "configuration": [{"eno1": {"address": "192.168.1.1"}}]}
        res = curl_post("http://127.0.0.1:5000/network_configuration/", json.dumps(dat), "wlp2")
        print(res)
    

    I'm leaving the question opened hopping that someone can give an answer using requests.

提交回复
热议问题