import sys
from http.server import HTTPServer, BaseHTTPRequestHandler
import urllib
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers() # 发送\r\n,意味这下一行为报体
# send html message,
self.wfile.write('hello, here you are'.encode())
return
if __name__ == '__main__': # 注意main后面没有括号
try:
server = HTTPServer(('', 8080), MyHandler)
server.serve_forever()
except KeyboardInterrupt:
print('^c received,shutting down the web server!')
server.socket.close()