2011-07-27 32 views
4

我對python相當陌生,所以我提前道歉,如果這是簡單的我很想念。我試圖將數據發佈到Python中的多部分形式。該腳本運行,但不會發布。我不確定我做錯了什麼。試圖在python中發佈多部分表單數據,不會發布

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)」,並沒有發佈任何數據:

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

海報在對數據進行編碼爲multipart/form數據,並可以在這裏找到:http://atlee.ca/software/poster/index.html

我發現用海報這裏的代碼:Using MultipartPostHandler to POST form-data with Python

如果任何人的好奇,我想一個在爲queXF(開源光學標記識別軟件)生成後,自動發佈pdf和xml帶區文件。 http://quexf.sourceforge.net/

+0

''bandingxml':'/ banding.xml' - 您需要在此行上使用open('/ banding.xml')',以匹配上面的行? –

+0

我確實需要開放;然而,我已經嘗試過,沒有開放。它仍然不會發布 – abclg

+0

您是否嘗試將MAX_FILE_SIZE及其值添加到您的值中?服務器可能會期待它。 – Gerrat

回答

1
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似乎已經解決了。