My code is simple. Using BaseHTTPServer and ThreadInMix I want to run a python script (Script1.py) f开发者_开发知识库or every request made simultaneously.
My Code-
from subprocess import PIPE, Popen
from SocketServer import ThreadingMixIn
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import time
def simple_script(self):
print 'simple_script'
s = Popen('C:/Python27/python C:/Script1.py 5', shell=True,
stdout=PIPE, stderr=PIPE)
out, err = s.communicate()
print out, err
self.wfile.write(out)
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write('{0}\n'.format(time.asctime()))
simple_script(self)
return
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
pass
if __name__ == '__main__':
server = ThreadedHTTPServer(('', 8080), Handler)
print 'Starting server, use <Ctrl-C> to stop'
server.serve_forever()
"""
# C:/Script1.py
import time, sys
s = time.time()
while True:
if time.time() - s > int(sys.argv[1]):
break
else:
time.sleep(1)
print time.asctime()
"""
I just found out that- With URL: http://localhost:8080 If I open multiple tabs/browsers for IE, this works JUST FINE But,
If I open multiple tabs/pages in Chrome or Firefox, the pages wait for previous page? This does not imply threading in Chrome or Firefox? Any help? Thanks
Works just fine for me:
Starting server, use to stop
localhost.localdomain - - [03/Oct/2011 16:25:55] "GET / HTTP/1.1" 200 -
simple_script
localhost.localdomain - - [03/Oct/2011 16:25:55] "GET / HTTP/1.1" 200 -
simple_script
Mon Oct 3 16:25:56 2011
Mon Oct 3 16:25:57 2011
Mon Oct 3 16:25:58 2011
Mon Oct 3 16:25:59 2011
Mon Oct 3 16:26:00 2011
Mon Oct 3 16:26:01 2011
Mon Oct 3 16:25:56 2011
Mon Oct 3 16:25:57 2011
Mon Oct 3 16:25:58 2011
Mon Oct 3 16:25:59 2011
Mon Oct 3 16:26:00 2011
Mon Oct 3 16:26:01 2011
精彩评论