尝试在python中发布多部分表单数据,不会发布 [英] Trying to post multipart form data in python, won't post

查看:91
本文介绍了尝试在python中发布多部分表单数据,不会发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对python还是很陌生,所以如果我错过了一些简单的事情,我会提前道歉.我正在尝试将数据发布到python中的多部分表单中.该脚本将运行,但不会发布.我不确定自己在做什么错.

I'm fairly new to python, so I apologize in advance if this is something simple I'm missing. I'm trying to post data to a multipart form in python. The script runs, but it won't post. I'm not sure what I'm doing wrong.

import urllib, urllib2
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers

def toqueXF():
    register_openers()
    url = "http://localhost/trunk/admin/new.php"
    values = {'form':open('/test.pdf'),
              'bandingxml':open('/banding.xml'),
              'desc':'description'}
    data, headers = multipart_encode(values)
    request = urllib2.Request(url, data, headers)
    response = urllib2.urlopen(request)
    the_page = response.read()
    print the_page

当我打电话给我时,打印结果会显示页面的html,就好像我运行了"urllib2.urlopen(url)"并且没有发布任何数据一样:

When I call this, the print gives me the html of the page, as if I ran "urllib2.urlopen(url)" and didn't post any data:

<form enctype="multipart/form-data" action="" method="post">
    <p><input type="hidden" name="MAX_FILE_SIZE" value="1000000000" /></p>
    <p>Select PDF file to create form from: <input name="form" type="file" /></p>
    <p>(Optional): Select banding XML file: <input name="bandingxml" type="file" /></p>
    <p>Enter description of form: <input name="desc" type="text"/><br/></p>
    <p><input type="submit" value="Upload form" /></p>
</form>

海报用于将数据编码为多部分/表单数据,可以在以下位置找到: http: //atlee.ca/software/poster/index.html

poster is to encode the data to multipart/form data and can be found here: http://atlee.ca/software/poster/index.html

我在这里找到了使用海报的代码:使用MultipartPostHandler通过Python发布表单数据

I found the code to use poster here: Using MultipartPostHandler to POST form-data with Python

如果有人好奇,我想在为queXF(一种开源光学标记识别软件)生成它们后自动发布pdf和xml带文件. http://quexf.sourceforge.net/

If anyone's curious, I'm trying to automatically post a pdf and xml banding file after they're generated for queXF (an opensource optical mark recognition software). http://quexf.sourceforge.net/

推荐答案

import urllib, urllib2
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers

def queXF():
    register_openers()
    url = "http://lilix2/trunk/admin/new.php"
    values = {'form':open('test.pdf'),
          'bandingxml':open('banding.xml'),
          'desc':'description'}
    data, headers = multipart_encode(values)
    headers['User-Agent'] = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
    request = urllib2.Request(url, data, headers)
    request.unverifiable = True
    response = urllib2.urlopen(request)
    the_page = response.read()

添加headers['User-Agent']request.unverifiable = True似乎已解决问题.

Adding headers['User-Agent'] and request.unverifiable = True seems to have fixed it.

这篇关于尝试在python中发布多部分表单数据,不会发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆