-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_python_server.py
63 lines (54 loc) · 1.06 KB
/
php_python_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import socket
import multiprocessing as mp
import dill
import os
import traceback
import time
import sys
def execute(conn,dill,sys,traceback):
try:
import time
commands=conn.recv(2**24)
print "\n",commands,"\n"
file2=conn.makefile('w',0)
old=sys.stdout
sys.stdout=file2
exec(commands)
sys.stdout=old
try:conn.shutdown(1)
except: pass
file2.close()
conn.close()
#print "conn is closed",conn
return
except Exception as e:
print "ERROR:"
print traceback.format_exc()
try:conn.shutdown(1)
except: pass
sys.stdout=old
file2.close()
conn.close()
print "Error"
return
def cleanup():
cleanup.called=1
cleanup.called=0
try:
if (len(sys.argv)>1):
port=int(sys.argv[1])
else:
port=45555
s=socket.socket()
print "PhP python listening on",port
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('',port))
s.listen(5)
while True:
conn,addr=s.accept()
print "Connection from",addr
p=mp.Process(target=execute,args=(conn,dill,sys,traceback))
p.start()
except:
print "ERROR:\n",traceback.print_exc()
s.close()