Python client server dilemma
Question: I'm trying to implement a project, where the python code will be written on
web-browser and then executed in a remote server. The arch is Javascript -->
Java --> python

The python code will be sent to java using web sockets, which is connected to
a python server using a TCP/IP socket. The script needs to be read line by
line from the socket using readLine and executed. It would be great if someone
can tell me how to run python commands within a python script. Is there a
better way to do it, like for example, save it as a file and run the the
entire script and send the output back to Java?

For example, I want to execute the following from the socket as I read it
using readLine...

    
    
    import pylab as pl 
    import numpy as np
    y = randn(100)
    pl.plot(y)
    pl.savefig('foo.png', bbox_inches='tight)
    

I have written the TCP/IP socket which gets the data from the java client

Any help here would be appreciated.


Answer: just put it in a variable as a string and use exec:

    
    
    tmp_str = '''
    print "Hello World!"
    print "another hello world!"
             '''
    
    exec tmp_str
    

