2016-12-29 61 views
0

請查找以下代碼,因爲它會拋出錯誤 - global name 'webserver' is not defined全球名稱'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): 
     global WebServer 
     """ 
     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): 
     global WebServer 
     """ 
     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") 
+0

你從來沒有分配任何價值'WebServer',所以'全球WebServer'是給你一個錯誤。只要刪除線,它應該工作正常! –

回答

0

您的代碼中不需要兩行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) 
+1

'create_web_server'是'WebServer'類中的一個方法,因此從'main'調用它仍然會失敗 – xbonez

+0

@xbonez好的捕獲 - 在看了這段代碼之後,它需要一些修改(並且可能會刪除一些方法)。 – birryree