如何更改Python的内容类型

如何更改Python的内容类型,python,post,content-type,Python,Post,Content Type,我想将文件上载到远程设备。 如果我查一下wireshark的连接,我会得到这个 POST /saveRestore.htm.cgi HTTP/1.1 Host: 10.128.115.214 User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:15.0) Gecko/20100101 Firefox/15.0.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*

我想将文件上载到远程设备。 如果我查一下wireshark的连接,我会得到这个

POST /saveRestore.htm.cgi HTTP/1.1
Host: 10.128.115.214
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:15.0) Gecko/20100101 Firefox/15.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://10.128.115.214/saveRestore.htm
Cache-Control: max-age=0
Content-Type: multipart/form-data; boundary=---------------------------961265085509552220604142744
Content-Length: 10708

-----------------------------961265085509552220604142744
Content-Disposition: form-data; name="restore"; filename="config(2).cfg"
Content-Type: application/octet-stream
这说明浏览器只接受text/html、application/xhtml+xml、application/xml;q=0.9,/;q=0.8

如果我用脚本上传文件,它会说

--0a7125aebb8845ba8ab9aa21306b01f6
Content-Disposition: form-data; name="restore"; filename="Config.cfg"
Content-Type: text/plain; charset=utf-8
所以它是一个错误的文件类型

那么如何更改文件的内容类型呢

我的代码如下所示:

#!/usr/bin/python

import httplib
import urllib2
from poster.encode import multipart_encode
import poster
from poster.streaminghttp import register_openers
register_openers()

params = {'restore': open("Config.cfg", "rb"), 'upload': 'PC ==>; Unit'}

datagen, headers = multipart_encode(params)

request = urllib2.Request('http://10.128.115.214/saveRestore.htm.cgi', datagen, headers)
u = urllib2.urlopen(request)
print u.read()
在报告中说:

如果设置了
filetype
,则将其用作此参数的内容类型。如果未设置,则默认为“text/plain;charset=utf8”

因此,不要像这样指定参数:

params = {'restore': open("Config.cfg", "rb"), 'upload': 'PC ==>; Unit'}
params = [MultipartParam('restore', open("Config.cfg", "rb"),
                         filetype = 'application/octet-stream'),
          ('upload', 'PC ==>; Unit')]
如下所示指定它们:

params = {'restore': open("Config.cfg", "rb"), 'upload': 'PC ==>; Unit'}
params = [MultipartParam('restore', open("Config.cfg", "rb"),
                         filetype = 'application/octet-stream'),
          ('upload', 'PC ==>; Unit')]

这可能的重复是完全不清楚的。什么是服务器,什么是客户端,在哪里设置内容类型/配置?服务器是10.128.115.214 saveRestore.htm.cgi是用于上载和还原文件的Web界面。。。我试着用一个脚本,而不是通过网络界面来实现。。。如果我用webinterface进行上传,1 wireshark就会出现。。。当我使用脚本执行此操作时,文件的内容类型是错误的