簡體   English   中英

在Python中更改內容類型

[英]Changing Content-Type in Python

因此,我嘗試在遠程設備上上傳文件。

如果我使用代碼:

#!/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()

使用Content-Type文本/純文本..上傳文件。

那么,如何將這種內容類型更改為text / html?

加:

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

在實例化您的Request對象之前。

您可以從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