TCS WINGS1 T4: Mini Project- Integrating Distributed Ecosystem (62649) ALL TEST CASES PASSED WORKING
TechTalk with Shaddy
0:00 / 0:00
TCS WINGS1 T4: Mini Project- Integrating Distributed Ecosystem (62649) ALL TEST CASES PASSED WORKING
13 060 просмотров · 2 г. назад
TechTalk with Shaddy
170 подписчиков
13 060 просмотров · 2 г. назад
Wings1 T4: Mini Project REST Server: Integrating Distributed Ecosystem (62649) All test cases passed and working #tcs #wings1 #rest
Code for direct copy paste:
from http.server import BaseHTTPRequestHandler, HTTPServer
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/get':
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b"GET request received")
elif self.path == '/redirect':
self.send_response(302)
self.send_header('Location', 'http://www.example.com')
self.end_headers()
elif self.path == '/divide_by_zero':
This will intentionally raise a ZeroDivisionError
result = 1 / 0
else:
self.send_response(404)
self.end_headers()
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
if self.path == '/post':
if post_data.decode('utf-8') == '{"username":"user", "password": "pass"}':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(b"POST request received with correct credentials")
else:
self.send_response(401)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(b"Unauthorized")
else:
self.send_response(404)
self.end_headers()
def run(server_class=HTTPServer, handler_class=SimpleHTTPRequestHandler, port=5000):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print("Starting server on port {}...".format(port))
httpd.serve_forever()
if _name_ == "__main__":
run()