Using Python 2.7
Situation:
I have some piece of Python code (that the user submitted) that i need to execute on my server (running on Django) and return the result. (Don’t worry about the security risk, it need to pass a security checkpoint first).
Suppose the python code is stored in the variable newCode
I do the following:
code = compile(newCode, '<string>', 'exec')
exec code
Now Suppose the newCode piece was:
a = raw_input('wa ')
print a
The server will freeze at this point.
The input to any program is stored in the database. It should read data from the database and plugged into the program when input or raw_input is called. The input process should be automated.
Any way of streaming the inputs to stdin before running the exec and have it read it during execution?
Pleeaase help! Thanks in advance. 🙂
Replace
sys.stdinwith aStringIOcontaining the text to read in.