As a forward to my answer, whitelisting and blacklisting only go so far and are proven easily broken by the most determined of hackers. Don't bother with these styles of security.
About as safe as you are going to get is to use pypy-sandbox it creates an OS level sandbox and tries to isolate processes that could lead to nasty execution.
For real security you probably want something more like this following model.
- Using SELinux as the host fire up a virtual machine running SELinux
- Disable all ports except for SSH and ensure patches are up to date
- Upload the code to a non executable directory.
- Chroot and ulimit all the things
- Execute the code through pypy-sandbox
- Destroy the machine when execution is complete
Or maybe I am just paranoid.