ich schreibe grad an einer Hühnerstall Software und möchte einen Webserver für get und post einbauen. Bekomme es aber nicht in meinem Programm zum laufen.
Ich möchte mit meinem Programm Daten per http get/post austauschen.
Aber ich erhalte den Fehler:
TypeError: __init__() takes 1 positional argument but 4 were given
Code: Alles auswählen
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from http.server import BaseHTTPRequestHandler, HTTPServer
class Coop(BaseHTTPRequestHandler):
HOST_NAME = '192.168.0.63'
PORT_NUMBER = 9000
def __init__(self):
# init Webserver
server_class = HTTPServer
httpd = server_class((Coop.HOST_NAME, Coop.PORT_NUMBER), Coop)
print(time.asctime(), 'Server Starts - %s:%s' % (Coop.HOST_NAME, Coop.PORT_NUMBER))
httpd.serve_forever()
def do_HEAD(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
paths = {
'/foo': {'status': 200},
'/bar': {'status': 302},
'/baz': {'status': 404},
'/qux': {'status': 500}
}
if self.path in paths:
self.respond(paths[self.path])
else:
self.respond({'status': 500})
def handle_http(self, status_code, path):
self.send_response(status_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
content = '''
<html><head><title>Title goes here.</title></head>
<body><p>This is a test.</p>
<p>You accessed path: {}</p>
</body></html>
'''.format(path)
return bytes(content, 'UTF-8')
def respond(self, opts):
response = self.handle_http(opts['status'], self.path)
self.wfile.write(response)
if __name__ == "__main__":
coop = Coop()