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/
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
Further complicating matters (for me), it appears some of the socket
programming code is in C, and my C skills are lacking.
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
Since that works, this also works (any socket operations appear to
raise an IOError):
import socket
socket.setdefaulttimeout(0)
Thanks!
-Adam
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.