views:

22

answers:

1

how to get results from exec() in python 3.1?

#!/usr/bin/python
import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = socket.gethostname()
port = 1234
sock.bind((host,port))

ret_str = "executed"

while True:
    cmd, addr = sock.recvfrom(1024)
    if len(cmd) > 0:
        print("Received ", cmd, " command from ", addr)
        exec(cmd) # here I need execution results returns to ret_str
        print( "results:", ret_str )
A: 

exec expression don't return a value use eval function insted.

print "result:", eval(cmd)
jcubic
thank you for your responce jcubic. but I need sometimes execute not only expressions, I need execute statements too. :(
66neo99