Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

disabling TCP connections, just for one script

1 view
Skip to first unread message

Adam Monsen

unread,
Sep 9, 2005, 12:41:06 PM9/9/05
to
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.

--
Adam Monsen
http://adammonsen.com/

Diez B. Roggisch

unread,
Sep 9, 2005, 1:42:02 PM9/9/05
to
Adam Monsen wrote:
> 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

Adam Monsen

unread,
Sep 9, 2005, 2:07:07 PM9/9/05
to
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.

cakebread

unread,
Sep 9, 2005, 4:01:17 PM9/9/05
to
How about this:

import timeoutsocket
timeoutsocket.setDefaultSocketTimeout(0)

This will make all sockets in your Python app fail.

https://svn.plone.org/svn/collective/CMFSquidTool/trunk/timeoutsocket.py

Adam Monsen

unread,
Sep 9, 2005, 4:36:04 PM9/9/05
to
Nice!!

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

import socket
socket.setdefaulttimeout(0)

Thanks!
-Adam

Adam Monsen

unread,
Sep 9, 2005, 5:03:58 PM9/9/05
to
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.

dify...@gmail.com

unread,
Sep 17, 2005, 5:00:45 PM9/17/05
to
>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.

0 new messages