Is there any method to connect to vpn through python and have that traffic of that application only route through the said VPN?
3 Answers
If you're adding the line # ip rule add from 192.168.0.0/24 table 10 then all traffic on your subnet will get pushed to the VPN. If you omit it and use only # ip rule add from 192.168.0.3/32 table 10 with the IPs of the specific IPs you want to have be VPN only it should work fine. If this is still an issue can you run route on the device and post the result? Sometimes you may need to route traffic through a specific gateway only for destinations matching a group of IPs or a subnet. Static routes are usually configured at the router level but you can also configure them locally, from the Windows command prompt. The operation is quite easy, you won’t miss the GUI.
This has nothing to do with Python. Routing issues are solely an issue of your networking configuration.
Python itself can't be used to route traffic; though you can use it to execute system commands to change your routing table. If you're on Linux, you need to use the ip
command from the iproute2 and iptables
from netfilter to change the routing behavior of specific traffic.
Please, be more specific in your question. Generally, yes, it is possible.
If you use python 2.7 or newer, you can use source_address
option for http connections (see reference for libraries you use) as tuple ('interface address', port)
.
If you use sockets in your app, use socket.bind(('interface address', port))
on created socket before socket.connect()
.
Not the answer you're looking for? Browse other questions tagged pythonvpn or ask your own question.
How do I route some traffic through a VPN?
I bought a pfSense router recently and I have to say it's the best device I've owned (from standard household routers, to dd-wrt, to a Sonicwall) in terms of power and flexibility. I've been able to replicate everything I've done before and even do some new things.
The one thing I'd really like to do is establish a VPN connection and route certain traffic through it, while other traffic uses the normal WAN connection (i.e. send my Netflix traffic through the vpn to bypass geoblocking)
If there are any guides on how to do this I'd appreciate being pointed in the right direction. I haven't had much luck with web searches on the subject.