-2

Why running the following code block gives an unbound error when a have globally assigned Html_code_1 and Html_code 2. So why python interpreter taking it as a local variable. I am using vagrant machine to run this code.

Following is the error message:

Exception happened during processing of request from ('10.0.2.2', 55545)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 290, in 
_handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 318, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 331, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 652, in __init__
    self.handle()
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
    self.handle_one_request()
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 328, in handle_one_request
    method()
  File "true.py", line 31, in do_GET
    Html_code_1 +="<ul>"

Here is the code:

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker


Html_code_1 = """
              <!Doctype html>
              <head><title>Foodpanda.com</title></head>
              <body>
"""    
Html_code_2 = """
              </body>
              </html>
"""

class webServerHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        try:
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            Html_code_1 +="<ul>"
            Html_code_1 += "</ul>"+Html_code_2
            output = Html_code_1
            self.wfile.write(output.encode())
            return
        except IOError:
            self.send_error(404, 'File Not Found: %s' % self.path)
def main():
    try:
        port = 8080
        server = HTTPServer(('', port), webServerHandler)
        print "Web Server running on port %s" % port
        server.serve_forever()
    except KeyboardInterrupt:
        print " ^C entered, stopping web server...."
        server.socket.close()

if __name__ == '__main__':
    main()
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • Please include your error message (as text) in your question, instead of linking to a screenshot. – khelwood Jul 26 '18 at 09:19
  • 1
    You should declare `global Html_code_1` inside `do_GET`, otherwise the interpreter assumes it is a local variable. – khelwood Jul 26 '18 at 09:20
  • 1
    Possible duplicate of [Local variable referenced before assignment in Python?](https://stackoverflow.com/questions/18002794/local-variable-referenced-before-assignment-in-python) – khelwood Jul 26 '18 at 09:22
  • Thanks, but my question is why when I don't run this is code in vagrant machine is just works fine. @khelwood – user8067411 Jul 26 '18 at 09:25
  • You've just added a different stack trace from the one you originally had in your screenshot. Are you asking about a different problem now? – khelwood Jul 26 '18 at 09:26
  • No sir, my question is the same. Sorry, I don't have explained my self correctly. My question is that why I have to declare that they are global when they are already global variables because they are declared outside the scope of every function and class. – user8067411 Jul 26 '18 at 09:36
  • See the dupe question already linked. If you assign to a variable inside a function, the interpreter assumes that you mean a local variable. That's why you have the `global` command to indicate that it is a global variable. – khelwood Jul 26 '18 at 09:37
  • I've edited your question to show the original stack trace you asked about, since that seems to be the one the question is about. – khelwood Jul 26 '18 at 09:40

1 Answers1

0

when you are calling do_GET function write

global Html_code_1

to make it usable in that function