- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有下一个 python 代码:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import os
PORT_NUMBER = 8080
#This class will handles any incoming request from
#the browser
class myHandler(BaseHTTPRequestHandler):
store_path = os.path.dirname(os.path.realpath(__file__)) + '\copyFile'
print store_path
# handler for the GET requests
def do_GET(self):
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
# Send the html message
self.wfile.write("Hello World !")
return
# handeler for POST request
def do_POST(self):
length = self.headers['content-length']
data = self.rfile.read(int(length))
with open(self.store_path, 'w') as fh:
fh.write(data.decode())
self.send_response(200)
try:
#Create a web server and define the handler to manage the
#incoming request
server = HTTPServer(('', PORT_NUMBER), myHandler)
print 'Started httpserver on port ' , PORT_NUMBER
#Wait forever for incoming htto requests
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down the web server'
server.socket.close()
我正在尝试制作一个简单的 python 网络服务器来保存发布到本地路径的文件。我使用 curl
将文件发送到服务器,其中包含以下行:curl -F file="myfile.txt"http://localhost:8080
结果和我想的不一样:
--------------------------e6929774a41d68c0
Content-Disposition: form-data; name="file"
myfile.txt
--------------------------e6929774a41d68c0--
我该怎么做才能解决这个问题?
我检查了this link但它没有帮助:(
最佳答案
好的,这里发生的事情是您已经启动了 HTTPServer,它创建了一个线程来监听传入的连接。当它获得有效连接时,它会创建一个请求处理程序的实例,该实例将根据传入连接的输入调用适当的方法。
BaseHTTPRequestHandler 将在后台处理传入的数据,并为您提供一些有用的变量来访问数据。正在发送的数据是从标准输入中检索的。因为 BaseHTTPRequestHandler 相当基础,所以它只能为您做这么多。来自该流的原始数据可以在 self.rfile 对象中找到。这是您会发现查询字符串、JSON 或二进制文件的地方。
现在您可以编写自己的解析器来检索这些数据,但这可能很复杂,并且已经有模块可以帮助您完成这项工作。有一个名为 cgi
的标准 Python 模块,可以使您更轻松地完成此操作。您可以找到有关此模块的信息 here .
您需要执行以下操作来检索您的文件:
import cgi
...
class myHandler(BaseHTTPRequestHandler):
...
def do_POST(self):
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={"REQUEST_METHOD": "POST",
"CONTENT_TYPE": self.headers['Content-Type']})
uploaded_file = form.getvalue("file")
if uploaded_file:
with open(self.store_path, "wb") as fh:
fh.write(uploaded_file.file.read())
...
BaseHTTPRequestHandler 实际上不会为您解析 POST 中的数据,因此我们需要为 FieldStorage 对象提供正确解析数据所需的信息。我们需要向它提供包含原始数据和请求中包含的 header 的文件。我们还需要为它提供包含请求详细信息的环境,因为 BaseHTTPRequestHandler 不解析 POST,这些变量不会添加到默认环境中,这就是我们创建自己的字典的原因。我会考虑看看 CGIHTTPServer它将封装一些这种行为。
一旦您创建了表单,我们就可以使用“安全”getter 来检索我们的数据。如果关键字不存在,则此方法将返回 None。您还可以使用类似的方式检索数据:
try:
uploaded_file = form['file']
except KeyError:
# The 'file' parameter was missing.
# Respond to the client with an error...
...
这两种方法都返回 FieldStorage 或 MiniFieldStorage 对象。这些的详细信息可以在 cgi 模块的链接中找到。但是 Python 中的一个 killer 级特性是 help
方法。例如
import cgi
help(cgi.FieldStorage)
这将列出一个手册页,提供您需要了解的有关 FieldStorage 对象的所有详细信息。
作为旁注,在 Python 中构建路径时,您最好使用 os.path.join()
为您正在运行的系统安全地创建有效路径。所以而不是有线:
store_path = os.path.dirname(os.path.realpath(__file__)) + '\copyFile'
我会将其替换为:
store_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'copyFile')
关于Python BaseHTTPServer 保存通过 curl 发送的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29349737/
今天我在一个 Java 应用程序中看到了几种不同的加载文件的方法。 文件:/ 文件:// 文件:/// 这三个 URL 开头有什么区别?使用它们的首选方式是什么? 非常感谢 斯特凡 最佳答案 file
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我有一个 javascript 文件,并且在该方法中有一个“测试”方法,我喜欢调用 C# 函数。 c# 函数与 javascript 文件不在同一文件中。 它位于 .cs 文件中。那么我该如何管理 j
需要检查我使用的文件/目录的权限 //filePath = path of file/directory access denied by user ( in windows ) File fil
我在一个目录中有很多 java 文件,我想在我的 Intellij 项目中使用它。但是我不想每次开始一个新项目时都将 java 文件复制到我的项目中。 我知道我可以在 Visual Studio 和
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我有 3 个组件的 Twig 文件: 文件 1: {# content-here #} 文件 2: {{ title-here }} {# content-here #}
我得到了 mod_ldap.c 和 mod_authnz_ldap.c 文件。我需要使用 Linux 命令的 mod_ldap.so 和 mod_authnz_ldap.so 文件。 最佳答案 从 c
我想使用PIE在我的项目中使用 IE7。 但是我不明白的是,我只能在网络服务器上使用 .htc 文件吗? 我可以在没有网络服务器的情况下通过浏览器加载的本地页面中使用它吗? 我在 PIE 的文档中看到
我在 CI 管道中考虑这一点,我应该首先构建和测试我的应用程序,结果应该是一个 docker 镜像。 我想知道使用构建环境在构建服务器上构建然后运行测试是否更常见。也许为此使用构建脚本。最后只需将 j
using namespace std; struct WebSites { string siteName; int rank; string getSiteName() {
我是 Linux 新手,目前正在尝试使用 ginkgo USB-CAN 接口(interface) 的 API 编程功能。为了使用 C++ 对 API 进行编程,他们提供了库文件,其中包含三个带有 .
我刚学C语言,在实现一个程序时遇到了问题将 test.txt 文件作为程序的输入。 test.txt 文件的内容是: 1 30 30 40 50 60 2 40 30 50 60 60 3 30 20
如何连接两个tcpdump文件,使一个流量在文件中出现一个接一个?具体来说,我想“乘以”一个 tcpdump 文件,这样所有的 session 将一个接一个地按顺序重复几次。 最佳答案 mergeca
我有一个名为 input.MP4 的文件,它已损坏。它来自闭路电视摄像机。我什么都试过了,ffmpeg , VLC 转换,没有运气。但是,我使用了 mediainfo和 exiftool并提取以下信息
我想做什么? 我想提取 ISO 文件并编辑其中的文件,然后将其重新打包回 ISO 文件。 (正如你已经读过的) 我为什么要这样做? 我想开始修改 PSP ISO,为此我必须使用游戏资源、 Assets
给定一个 gzip 文件 Z,如果我将其解压缩为 Z',有什么办法可以重新压缩它以恢复完全相同的 gzip 文件 Z?在粗略阅读了 DEFLATE 格式后,我猜不会,因为任何给定的文件都可能在 DEF
我必须从数据库向我的邮件 ID 发送一封带有附件的邮件。 EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Adventure Works Admin
我有一个大的 M4B 文件和一个 CUE 文件。我想将其拆分为多个 M4B 文件,或将其拆分为多个 MP3 文件(以前首选)。 我想在命令行中执行此操作(OS X,但如果需要可以使用 Linux),而
快速提问。我有一个没有实现文件的类的项目。 然后在 AppDelegate 我有: #import "AppDelegate.h" #import "SomeClass.h" @interface A
我是一名优秀的程序员,十分优秀!