global name 'WebServer' is not defined

915 views Asked by At

Please find below code as it is throwing the error - 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")
1

There are 1 answers

2
wkl On

You don't need the two global WebServer lines in your code. The global keyword is used to grant write access to a global variable, and you don't have a global variable named WebServer.

Your code should look like the following to resolve your error, though in its state, it will still throw errors because your code has multiple problems, including calling the create_web_server method by itself, as it's a method that needs to be run on a WebServer object.

I've included a working prototype example of your code at the bottom of my post.

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

Here is a simplified version of your code that should do the same thing (serve a directory with a basic SimpleHTTPRequestHandler).

It runs the thread in daemon mode so that it can be interrupted with Ctrl-C. I removed several methods as they didn't seem to serve a purpose in your original code, and to show you a basic framework of what your WebServer class would probably look like.

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)

And to take it a step further, you don't need to create a Thread subclass. It's allowed because it feels more 'natural' to developers who are familiar with Java, where subclassing Thread or implementing Runnable is standard practice.

You can just do something like:

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)