By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,285 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,285 IT Pros & Developers. It's quick & easy.

disabling TCP connections, just for one script

P: n/a
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/

Sep 9 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Sep 9 '05 #2

P: n/a
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.

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

Sep 9 '05 #3

P: n/a
How about this:

import timeoutsocket
timeoutsocket.setDefaultSocketTimeout(0)

This will make all sockets in your Python app fail.

https://svn.plone.org/svn/collective...meoutsocket.py

Sep 9 '05 #4

P: n/a
Nice!!

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

import socket
socket.setdefaulttimeout(0)

Thanks!
-Adam

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

Sep 9 '05 #5

P: n/a
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.

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

Sep 9 '05 #6

P: n/a
>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.

Sep 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.