简体   繁体   中英

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 ..

So how do I Change this content-type for example to text/html ?

Add:

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

Before instantiating your Request object.

You can use mechanize lib from python:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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