未定义全局名称"网络服务器"



请在丢弃错误时找到以下代码-global name 'webserver' is not defined

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")

您不需要代码中的两个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 - c 中断。我删除了几种方法,因为它们似乎在您的原始代码中没有目的,并向您展示了您的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)

相关内容

  • 没有找到相关文章

最新更新