gpt4 book ai didi

python - 如何使用 python 保存在 POST 请求中收到的文本文件

转载 作者:太空宇宙 更新时间:2023-11-03 16:42:57 25 4
gpt4 key购买 nike

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import SocketServer


class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()

def do_POST(self):
data = self.rfile.read(int(self.headers.getheader('content-length')))
print data
self._set_headers()
self.wfile.write("<html><body><h1>POST!</h1></body></html>")

def run(server_class=HTTPServer, handler_class=S, port=8080):
server_address = ('localhost', port)
httpd = server_class(server_address, handler_class)
print 'Starting server'
httpd.serve_forever()

run()

这是我的服务器代码。我收到的帖子请求包含一个文本文件和其他键:值对。这会打印整个内容,包括文本文件的内容。我怎样才能将该文本文件保存在我的硬盘上......?任何指导表示赞赏。

最佳答案

data = ['hey there']
with open('filehere.txt', 'w') as file:
for item in data:
file.write("%s\n" % item)

我希望这至少是某种指导方针。

关于python - 如何使用 python 保存在 POST 请求中收到的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36622564/

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