How to force specific network interface for outgoing connection in Python under Windows 7?
Asked Answered
C

0

7

I have two network adapters in Windows 7 environment, and both of them are connected to Internet:

  1. Ethernet Adapter (local address 10.9.187.251, external address 32.181.63.52)
  2. Wireless LAN Adapter (local address 192.168.1.178, external address 5.7.68.81)

Ethernet Adapter is used by default.

I want to make a connection with outgoing external address 5.7.68.81 through Wireless LAN in Python script.

I do following:

import pycurl, StringIO, re
c = pycurl.Curl()
c.setopt(pycurl.URL, 'http://myproxylists.com/my-http-headers')
c.setopt(pycurl.INTERFACE, "192.168.1.178")
b = StringIO.StringIO()
c.setopt(pycurl.WRITEFUNCTION, b.write)
c.setopt(pycurl.SSL_VERIFYPEER, False)
c.setopt(pycurl.SSL_VERIFYHOST, False)
c.perform()
rsp = b.getvalue()
mtch = re.search(r'REMOTE_ADDR == (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})', rsp)
print mtch.group(1)

And I get printout:

32.181.63.52

So default adapter was used. It seems like c.setopt(pycurl.INTERFACE, "192.168.1.178") doesn't work or I use it wrong way.

Also I tried all answers from this question with urllib2 and socket.bind(('192.168.1.178', 0) - the same result.

How do I specify network interface for outgoing connection using Python 2.7 in Windows 7?

Cogent answered 27/1, 2013 at 10:30 Comment(2)
Your code worked for me under RHEL 6, Python 2.7.2.Georgettegeorgi
Happy to hear this. I re-tried my own code, and recommendations from answer's questions (posters have deleted them), but no luck. I think, this is because my Home Premium version of Windows 7 doesn't allow selecting of network interface for outgoing connectionsCogent

© 2022 - 2024 — McMap. All rights reserved.