简体   繁体   English

在Python中更改内容类型

[英]Changing Content-Type in Python

So I try to upload a file on a Remote Device. 因此,我尝试在远程设备上上传文件。

If I use the Code: 如果我使用代码:

#!/usr/bin/python

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

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

datagen, headers = multipart_encode(params)

request = urllib2.Request('http://www.test.com/saveRestore.htm.cgi', datagen, headers)
u = urllib2.urlopen(request)
print u.read()

The file is Uploaded with the Content-Type text/plain .. 使用Content-Type文本/纯文本..上传文件。

So how do I Change this content-type for example to text/html ? 那么,如何将这种内容类型更改为text / html?

Add: 加:

headers['Content-Type'] = 'text/html'

Before instantiating your Request object. 在实例化您的Request对象之前。

You can use mechanize lib from python: 您可以从python使用机械化lib:

import mechanize
b = mechanize.Browser()

# Set any header you like:
b.addheaders = [('Content-Typoe', 'text/html; charset=utf-8')]
response = b.open('http://www.reddit.com')
data = response.read()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM