是否有一种非常简单的方法可以在Python 3.x中访问http请求的直接文本/字节?类似于你从Telnet或类似的东西。我正在寻找一些我可以设置来监听端口,接受请求并直接阅读所遇到的内容。它不会定义它正在寻找POST或GET等,只是原始值:
样本值:
GET /index.html/?=request HTTP/1.1
Host: www.example.com
User-Agent: Safari/4.0
答案 0 :(得分:0)
我正在寻找的图书馆是here。我正在寻找的代码是:
import socketserver
class MyTCPHandler(socketserver.BaseRequestHandler):
"""
The RequestHandler class for our server.
It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
print("{} wrote:".format(self.client_address[0]))
print(self.data)
# just send back the same data, but upper-cased
self.request.sendall(self.data.upper())
if __name__ == "__main__":
HOST, PORT = "localhost", 9999
# Create the server, binding to localhost on port 9999
server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()