2

Im triying to create a variable in init but it is not recognized in other method in the same class.

I don`t know why self.myvarlol is not working properly in send_myheaders() :S

CODE:

from http.server import HTTPServer, BaseHTTPRequestHandler
import time

class myserveromg(BaseHTTPRequestHandler):

    def __init__(self, a, b, c):
        BaseHTTPRequestHandler.__init__(self, a, b, c)
        self.myvarlol = "asdf"
        self.date = self.date_time_string()


    def send_my_headers(self):
        self.send_header("Content-type", "text/html")
        self.send_header("Date", self.date)
        self.end_headers()

    def do_GET(self):
        self.send_response_only(200)
        self.send_my_headers()
        self.wfile.write(bytes("<html><head><title>Title goes here.</title></head>", "utf-8"))
        self.wfile.write(bytes("<body><p>This is a test. </p>", "utf-8"))
        self.wfile.write(bytes("<p>You accessed path: %s</p>" % self.path, "utf-8"))
        self.wfile.write(bytes("</body></html>", "utf-8"))


if __name__ == "__main__":
    hostName = "localhost"
    hostPort = 9000

    appPortal = myserveromg
    myServer = HTTPServer((hostName, hostPort), appPortal)

    try:
        print(time.asctime(), "Server Starts - %s:%s" % (hostName, hostPort))
        myServer.serve_forever()
    except KeyboardInterrupt:
        pass

    myServer.server_close()
    print(time.asctime(), "Server Stops - %s:%s" % (hostName, hostPort))

ERROR:

File "C:\Users\Anonym-PC\Desktop\nuseke.py", line 14, in send_my_headers
    self.send_header("Date", self.date)
AttributeError: 'myserveromg' object has no attribute 'date'
Rakanitshu
  • 21
  • 2
  • 3
    You don't init an instance of the class. Try appPortal = myserveromg() – antonio_antuan Mar 21 '16 at 20:45
  • Ty for your answer! I have tried that, but I have this error now: Traceback (most recent call last): File "C:\Users\Anonym-PC\Desktop\nuseke.py", line 30, in appPortal = myserveromg() TypeError: __init__() missing 3 required positional arguments: 'a', 'b', and 'c' – Rakanitshu Mar 21 '16 at 20:58

1 Answers1

4

You say appPortal = myserveromg, but that just creates an alias. appPortal is now the same thing as myserveromg: a class. You need to make an instance which will call __init__ and define date. To do that, add parentheses:

appPortal = myserveromg(a, b, c) # a, b, and c will need to be defined earlier on.
zondo
  • 19,901
  • 8
  • 44
  • 83
  • Thanks for your answer! in that case I get this error Traceback (most recent call last): File "C:\Users\Anonym-PC\Desktop\nuseke.py", line 30, in appPortal = myserveromg() TypeError: __init__() missing 3 required positional arguments: 'a', 'b', and 'c' – Rakanitshu Mar 21 '16 at 20:57
  • @Rakanitshu: You just need to give it three arguments. You're the one who defined it, so you're the one who best knows which ones. – zondo Mar 21 '16 at 22:24
  • the three arguments are from "BaseHTTPRequestHandler" (Python libraries) and I dont know why if I dont define it in HTTPServer((hostName, hostPort), appPortal) it works :S – Rakanitshu Mar 21 '16 at 22:25
  • I haven't worked with that library, but my guess would be to use `myserveromg(hostName, hostPort, myServer)` – zondo Mar 21 '16 at 22:31