您的代碼中不需要兩行global WebServer
行。 global
關鍵字用於授予對全局變量的寫入權限,並且您沒有名爲WebServer
的全局變量。
你的代碼應該看起來像下面這樣解決你的錯誤,儘管在它的狀態下,它仍然會拋出錯誤,因爲你的代碼有多個問題,包括自己調用create_web_server
方法,因爲它是一個需要運行的方法在WebServer
對象上。
我已經在我的文章的底部包含了一個工作原型的代碼示例。
import SimpleHTTPServer
import SocketServer
import os
from threading import Thread
import threading
class WebServer(Thread):
def __init__(self, stream_path):
"""
Class to create a Web server and add given folder to the web server which is passed as an argument.
In this case it creates the web server to the incoming streams pushed by VLC to temp folder
"""
Thread.__init__(self)
self.stream_path = stream_path
def run(self):
"""
This method is built in Thread object method invoked by start()
and the code which is under run() will be executed.
"""
os.chdir(self.stream_path)
PORT = 8000
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
print "function thread", threading.currentThread()
httpd.serve_forever()
print "test1"
def create_web_server(self,stream_path):
"""
This method is to create the web server to a given path
"""
obj1 = WebServer(self,stream_path)
obj1.start()
print "server created"
def stop_web_server(self):
cmd='tskill python /A'
os.system(cmd)
if __name__ == '__main__':
create_web_server("","C:\\\\QED")
這裏是你的代碼的簡化版本,應該做同樣的事情(服務目錄與基本SimpleHTTPRequestHandler
)。
它運行在daemon
模式線程,以便它可以與按Ctrl被打斷 - Ç。我刪除了幾種方法,因爲它們似乎並未在您的原始代碼中提供用途,並向您展示了您的WebServer
類可能的外觀的基本框架。
import threading
import os
import time
import SimpleHTTPServer
import SocketServer
class WebServer(threading.Thread):
# Let's specify additional constructor arguments for host/port
# so you don't have to hardcode those values.
# We can specify default values for them as well.
def __init__(self, stream_path, host='localhost', port=8000):
super(WebServer, self).__init__()
self.stream_path = stream_path
self.host = host
self.port = port
# This is the method that will be called when you call
# .start() on a WebServer object.
def run(self):
os.chdir(self.stream_path)
handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer((self.host, self.port), handler)
httpd.serve_forever()
if __name__ == '__main__':
# Create a WebServer thread object, set it to daemonize
# and start it
ws = WebServer(r'C:\QED')
ws.daemon = True
ws.start()
# Since the main thread doesn't do anything after daemonizing
# the WebServer, it would exit immediately.
# This forever loop just ensures that the main thread runs
while True: time.sleep(1)
而且要更進一步,您不需要創建Thread
子類。這是允許的,因爲熟悉Java的開發人員感覺更自然,其中子類Thread
或實施Runnable
是標準慣例。
你可以做類似:
import threading
import os
import time
import SimpleHTTPServer
import SocketServer
def run_http_server(serve_dir, host='localhost', port=8000):
os.chdir(serve_dir)
handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer((host, port), handler)
httpd.serve_forever()
if __name__ == '__main__':
thread = threading.Thread(target=run_http_server, args=(r'C:\QED',))
thread.daemon = True
thread.start()
while True: time.sleep(1)
你從來沒有分配任何價值'WebServer',所以'全球WebServer'是給你一個錯誤。只要刪除線,它應該工作正常! –