disabling TCP connections, just for one script

A

Adam Monsen

It would be helpful to be able to temporarily disable AF_INET socket
connections (I don't care about IPv6 for now).

What I'd like to do is create an environment for a Python script that
appears like the network interface is down, so any remote socket stuff
should fail (like an HTTP GET request to Google's home page, for
example).

Anyone know an easy way to do that? Maybe by manipulating the socket
module or something? I'd still like to be able to access the internet
from other programs (like my Web browser, IM app, etc.) while I'm
working on this script, so unplugging the network cable or just
temporarily shutting down networking for the whole system aren't really
acceptable solutions.

Maybe chroot would be useful, but this seems a little complicated for
what I'm trying to do. Setting up some kind of Xen or UML (user mode
Linux) environment, and just disabling networking within that virtual
machine would possibly work, too.
 
D

Diez B. Roggisch

Adam said:
It would be helpful to be able to temporarily disable AF_INET socket
connections (I don't care about IPv6 for now).

Maybe put an socket.py with mockup funktions in your path before the
real socket.py. Then you can alter teh behaviour of all necessary functions.

Diez
 
A

Adam Monsen

I know very little about socket programming and even less about
sockey.py... any idea what behavior would have to be modified?

Further complicating matters (for me), it appears some of the socket
programming code is in C, and my C skills are lacking.
 
A

Adam Monsen

Nice!!

Since that works, this also works (any socket operations appear to
raise an IOError):

import socket
socket.setdefaulttimeout(0)

Thanks!
-Adam
 
A

Adam Monsen

Another solution is using a deliberately misconfigured proxy:

import socket
socket.setdefaulttimeout(1)
import urllib
proxies = {'http': 'http://www.example.com:3128'}
url = 'http://www.google.com/'
opener = urllib.FancyURLopener(proxies)
f = opener.open(url)
print f.read()

From this code I get the following exception:
"IOError: [Errno socket error] timed out"

....and that fits my needs nicely.
 
D

dify.ltd

From the post I gather that you are looking for a solution under Linux.
I don't know much linux, but under Windows you could patch the IAT
(import address table) of the running process temporarly to redirect
calls to the socket winsock api to a stup function, which simply fails.
If you are interested, drop me a letter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top