gpt4 book ai didi

python - 如何可视化 BaseHTTPRequestHandler 的主体部分

转载 作者:可可西里 更新时间:2023-11-01 17:17:04 26 4
gpt4 key购买 nike

我正在用 Python 编写一个服务器,我想在 GET 请求的正文部分发送一些数据。目前,当我运行我的代码时,它只是卡住。

我试着把它放在 try/except 中并阅读有关实例变量 rfile 的详细信息,但我没有发现任何有用的东西

客户端:

import http.client
import sys
import os

#get http server ip
http_server = sys.argv[1]
#create a connection
conn = http.client.HTTPConnection(http_server)

while 1:
cmd = input('input command (ex. GET index.html): ')
cmd = cmd.split()

f = open('data.txt')

if cmd[0] == 'exit': #tipe exit to end it
break

#request command to server
conn.request(cmd[0],'',f.read())

#get response from server
rsp = conn.getresponse()

#print server response and data
print(rsp.status, rsp.reason)
data_received = rsp.read()
print(data_received)

服务器端:

from http.server import BaseHTTPRequestHandler,HTTPServer
import os

class TestHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
a = ''
fichier = open("data2.txt", "w")
try:
fichier.write(self.rfile.read())
except:

self.send_response(200)
self.send_header('Content-type','text-html')
self.end_headers()
return


def run():
print('http server is starting...')
server_address = ('127.0.0.1',80)
httpd = HTTPServer(server_address, PancakeHTTPRequestHandler)
print('htttp server is running...')
httpd.serve_forever()

if __name__ == '__main__':
run()

我希望能够将我的 GET 请求中的数据写入我的 data2.txt 文件中。

谢谢你的帮助

最佳答案

它因为 server.py 中的 self.rfile.read() 而卡住。 read 方法期望读取 EOF 或字节长度。参见 https://docs.python.org/3/library/io.html#io.BufferedIOBase.read您正在尝试记录对服务器发出的客户端请求,一个快速的解决方法是将请求的内容长度传递给带有 int(self.headers.get('Content-Length') )

最后它给了我们:

客户端.py

import http.client
import sys
import os

#get http server ip
http_server = sys.argv[1]
#create a connection
conn = http.client.HTTPConnection(http_server)

while 1:
cmd = input('input command (ex. GET index.html): ')
cmd = cmd.split()

f = open('data.txt')

if cmd[0] == 'exit': #tipe exit to end it
break

#request command to server
conn.request(cmd[0], '', f.read())

#get response from server
rsp = conn.getresponse()

#print server response and data
print(rsp.status, rsp.reason)
data_received = rsp.read()
print(data_received)

服务器.py

from http.server import BaseHTTPRequestHandler,HTTPServer
import os

class TestHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
a = ''
fichier = open("data2.txt", "a")
try:
content_length = int(self.headers.get('Content-Length'))
response_str = self.rfile.read(content_length)
fichier.write(response_str.decode('utf-8'))
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
except:
self.send_response(200)
self.send_header('Content-type','text-html')
self.end_headers()
fichier.close()
return


def run():
print('http server is starting...')
server_address = ('127.0.0.1',80)
httpd = HTTPServer(server_address, TestHTTPRequestHandler)
print('htttp server is running...')
httpd.serve_forever()

if __name__ == '__main__':
run()

附言。我不知道 PancakeHTTPRequestHandler 是什么,所以我用 TestHTTPRequestHandler 替换了它。我还在 try 中添加了一个响应,除了在服务器端,以便客户端获得响应,否则它会崩溃。

关于python - 如何可视化 BaseHTTPRequestHandler 的主体部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56192134/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com