Python3 ning ichki imkoniyatlaridan foydalanib oddiy web ilova ko'tarish mumkin (framework yoki kutubxona ishlatmasdan)!
Buning uchun biz http va socketserver modullaridan foydalanishimiz yetarli va u quyidagicha bo'ladi:
from http.server import SimpleHTTPRequestHandler
from http import HTTPStatus
from socketserver import TCPServer
class Handler(SimpleHTTPRequestHandler):
def do_GET(self):
path = self.path
if path == "/":
status = HTTPStatus.OK
content = b"Bosh sahifa"
else:
status = HTTPStatus.NOT_FOUND
content = b"Sahifa topilmadi"
self.send_response(status)
self.end_headers()
self.wfile.write(content)
if __name__ == "__main__":
ip_address, port = '127.0.0.1', 8000
httpd = TCPServer((ip_address, port), Handler)
print(f"Server ishga tushdi: http://{ip_address}:{port}")
httpd.serve_forever()
Tekshirib ko'rish uchun brovserda ochib ko'rish yoki curl'dan ham foydalanishimiz mumkin :
$ curl http://127.0.0.1:8000
Bosh sahifa
$ curl http://127.0.0.1:8000/yangiliklar
Sahifa topilmadi
@uzpythonlogs
#web_ilova #http #TcpServer
Buning uchun biz http va socketserver modullaridan foydalanishimiz yetarli va u quyidagicha bo'ladi:
from http.server import SimpleHTTPRequestHandler
from http import HTTPStatus
from socketserver import TCPServer
class Handler(SimpleHTTPRequestHandler):
def do_GET(self):
path = self.path
if path == "/":
status = HTTPStatus.OK
content = b"Bosh sahifa"
else:
status = HTTPStatus.NOT_FOUND
content = b"Sahifa topilmadi"
self.send_response(status)
self.end_headers()
self.wfile.write(content)
if __name__ == "__main__":
ip_address, port = '127.0.0.1', 8000
httpd = TCPServer((ip_address, port), Handler)
print(f"Server ishga tushdi: http://{ip_address}:{port}")
httpd.serve_forever()
Tekshirib ko'rish uchun brovserda ochib ko'rish yoki curl'dan ham foydalanishimiz mumkin :
$ curl http://127.0.0.1:8000
Bosh sahifa
$ curl http://127.0.0.1:8000/yangiliklar
Sahifa topilmadi
@uzpythonlogs
#web_ilova #http #TcpServer