Sandboxing
You will need support from the operating system to effectively sandbox an application.
- On FreeBSD, you can use a jail. This has proven to be quite secure over the years. (While there have been vulnerabilities in this system, the concensus is that it is not possible for a program to break out of a jail without outside help. <1>)
- On Linux you can use LXC.
- On MS-Windows you could use sandboxie.
Have a look at the comparison of virtualization technologies before deciding what to do. Personally I'd suggest only using technologies that offer "root privilege isolation".
Another possibility would be to use a virtual machine. But that would probably have more overhead.
And no matter what you use, you still need the firewall on the host to redirect some traffic to the sandbox/virtual machine.
Python security
CPython itself
The source code for the standard CPython is regularly audited by coverity. In their 2012 scan they found 0.005 defects in 1000 lines of code. The average for open source projects is 0.69 defects per 1000 lines, and 1 defect/1000 lines is accepted as a good industry standard.
So CPython itself doesn't have many defects.
Web programming with Python
The OWASP Python security project has identified security concerns in the CPython source code, as well as security concerns in modules and functions.
The built-in eval() function deserves special mention here.
It executes the Python code given to it as a string without check or boundaries. So while it is sometimes very useful, this function should never ever be given untrusted input from the web!
For instance, don't be tempted to use it to give your web app a built in calculator.
Their top-10 list of web app vulnerabilities also makes for interesting reading.
<1> From the documentation;
Jails are a powerful tool, but they are not a security panacea. While it is not possible for a jailed process to break out on its own, there are several ways in which an unprivileged user outside the jail can cooperate with a privileged user inside the jail to obtain elevated privileges in the host environment.
Most of these attacks can be mitigated by ensuring that the jail root is not accessible to unprivileged users in the host environment. As a general rule, untrusted users with privileged access to a jail should not be given access to the host environment.