Work on cgi bin stuff
[progcomp2013.git] / web / qchess.cgi
1 #!/usr/bin/python
2
3 # CGI wrapper to qchess
4
5 import sys
6 import os
7
8 import cgi
9 import subprocess
10
11 def main(argv):
12         form = cgi.FieldStorage()
13         client = cgi.escape(os.environ["REMOTE_ADDR"])
14         
15         try:
16                 with open(client): pass
17         except IOError:
18                 args = ["python", "../qchess/qchess.py", "--no-graphics", "@fifo:"+client, "@internal:AgentBishop"]
19                 subprocess.Popen(args)
20                 form["mode"] = "query"
21         
22         if form["mode"] == "response":
23                 x = int(form["x"])
24                 y = int(form["y"])
25                 fifo_out = open(client+".in", "w")
26                 fifo_out.write("%d %d\n" % (x, y))
27                 fifo_out.close()
28                 form["mode"] = "query"
29         
30                 
31         if form["mode"] == "query":
32                 fifo_in = open(client+".out", "r")
33                 s = fifo_in.readline().strip(" \r\n")
34                 while s != "SELECT?" and s != "MOVE?" and s.split(" ")[0] != "white" and s.split(" ")[0] != "black":
35                         print s
36                         s = fifo_in.readline().strip(" \r\n")
37                 print s
38                 fifo_in.close()
39                 form["mode"] = "response"
40                 
41                 if s == "quit":
42                         os.remove(client)
43                         
44                 
45         return 0
46
47
48 if __name__ == "__main__":
49         try:
50                 sys.exit(main(sys.argv))
51         except, e:
52                 print "Exception: ", e
53                 sys.exit(1)

UCC git Repository :: git.ucc.asn.au