simplepython起一个http
简单的Python代码可以起一个HTTP服务器,可以用于处理HTTP请求和响应。
from http.server import BaseHTTPRequestHandler, HTTPServer
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b'Hello, World!')
def run(server_class=HTTPServer, handler_class=SimpleHTTPRequestHandler, port=8000):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print('Starting server...')
httpd.serve_forever()
if __name__ == '__main__':
run()
这个简单的HTTP服务器会监听本地的8000端口,并在收到GET请求时返回"Hello, World!"。你可以在浏览器中访问http://localhost:8000来查看服务器的响应
原文地址: https://www.cveoy.top/t/topic/iUhc 著作权归作者所有。请勿转载和采集!