我想用cherrypy做一个python代码。目标是从UI调用脚本,当脚本在控制台上生成日志时,在UI(Iframe / div)上打印它。 我使用的是CherryPy-3.2.4和Python 2.7。 以下是我写的拒绝工作的示例代码。
TestProcess.py
from multiprocessing import Process, Queue
import subprocess
import os, os.path
from string import Template
import cherrypy
from multiprocessing import Process, Queue
import HTML
import TestScript
class TestProcess(object):
PID=0
jquery_url = 'C:\CherryPy\TestScripts\jquery\jquery-1.11.1.js'
q=Queue()
@cherrypy.expose
def index(self):
html = """\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Test Doc </TITLE>
<script type="text/javascript" src="/static/jq/jquery-1.11.1.js"></script>
</HEAD>
<BODY>
<BR/>
<h3>Test utility</h3>
<form id="ping_form" target="console_iframe" method="post" action="/f">
<button id="ping" type="submit">Upgrade Installed Releases</button>
</form>
<BR/>
<iframe name="console_iframe" frameborder="1" width="1200"/>
</BODY>
</HTML>
"""
t = Template(html)
page = t.substitute(jquery_url=self.jquery_url)
return page
def f1():
print "*********** TEST **********************"
q.put([42, None, 'hello'])
@cherrypy.expose
def f(self, **kw):
q=Queue()
ts = TestScript.TestScript()
p = Process(target=ts.testCallFunction, args=(q,))
p.start()
#print q.get() # prints "[42, None, 'hello']"
p.join()
print "Test F"
def run_command():
# The yeilds here are the key to keeping things streaming
yield '<style>body {font-family: monospace;}</style>'
while(p.is_alive()):
while(not q.empty()):
yield q.get_nowait()
while(not q.empty()):
yield q.get_nowait()
return run_command()
if __name__ == '__main__':
conf = {
'/': {
'tools.sessions.on': True,
'tools.staticdir.root': os.path.abspath(os.getcwd())
},
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': './public'
}
}
q = Queue()
cherrypy.server.socket_host = '10.49.69.103'
cherrypy.config.update({
'log.screen':True,
'tools.sessions.on': True,
'checker.on':False
})
cherrypy.tree.mount(TestProcess(), config=None)
cherrypy.engine.start()
cherrypy.engine.block()
#cherrypy.quickstart(TestProcess(), '/', conf)
Class TestScript
class TestScript(object):
q=Queue()
def write_queue(self,data):
print "*********** TEST **********************"
scroll_to_bottom = '<script type="text/javascript">window.scrollBy(0,50);</script>'
if data == '\n':
self.q.put("\n<br />%s" % scroll_to_bottom) # include the iframe scroll fix
else:
self.q.put(data)
def testCallFunction(self, q):
#proc = subprocess.Popen(['python','CreateLog.py'],stdout=subprocess.PIPE)
cmd = subprocess.Popen(['python','CreateLog.py'], shell=True, stdout=subprocess.PIPE)
while True:
data = cmd.stdout.read(1) # Alternatively proc.stdout.read(1024)
if len(data) == 0:
break
self.write_queue(data) # sys.stdout.buffer.write(data) on Python 3.x
#print "*********** TEST **********************"
脚本CreateLog.py
#filters output
import time
i = 0
while (i<=10):
print hex(i)*512
i += 1
time.sleep(0.5)
运行时得到的错误
[24/Jul/2014:16:59:10] ENGINE Bus STARTING
[24/Jul/2014:16:59:10] ENGINE Started monitor thread 'Autoreloader'.
[24/Jul/2014:16:59:10] ENGINE Started monitor thread '_TimeoutMonitor'.
[24/Jul/2014:16:59:15] ENGINE Error in 'start' listener <bound method Server.sta
rt of <cherrypy._cpserver.Server object at 0x01583390>>
Traceback (most recent call last):
File "c:\Python27\lib\site-packages\cherrypy\process\wspbus.py", line 197, in
publish
output.append(listener(*args, **kwargs))
File "c:\Python27\lib\site-packages\cherrypy\_cpserver.py", line 151, in start
ServerAdapter.start(self)
File "c:\Python27\lib\site-packages\cherrypy\process\servers.py", line 168, in
start
wait_for_free_port(*self.bind_addr)
File "c:\Python27\lib\site-packages\cherrypy\process\servers.py", line 412, in
wait_for_free_port
raise IOError("Port %r not free on %r" % (port, host))
IOError: Port 8080 not free on '10.49.69.103'
[24/Jul/2014:16:59:15] ENGINE Shutting down due to error in start listener:
Traceback (most recent call last):
File "c:\Python27\lib\site-packages\cherrypy\process\wspbus.py", line 235, in
start
self.publish('start')
File "c:\Python27\lib\site-packages\cherrypy\process\wspbus.py", line 215, in
publish
raise exc
ChannelFailures: IOError("Port 8080 not free on '10.49.69.103'",)
我做错了什么?
答案 0 :(得分:1)
确保新的Python解释器可以安全地导入主模块,而不会导致意外的副作用(例如启动新进程)。
您没有用if __name__ == '__main__':
保护TestProcess.py的最后一行,因此Process
尝试运行另一台服务器。
答案 1 :(得分:0)
端口8080不是免费的,因此HTTP服务器无法监听它。尝试netstat -anpt
查看正在使用该端口的内容,或尝试“server.socket_port:8081”或其他一些免费的端口。