2014-09-23 53 views
0

我正在使用MultipartPostHandler library创建多部分表单数据请求。Python - 多部分表单数据无法使用MultipartPostHandler库进行身份验证

我遇到了问题,可能需要在库中进行更新。我们使用lighttpd web服务器,我们的cgi-bin文件夹受到用户名和密码的保护。

每当我们需要使用该文件夹中的cgi文件时,我们需要使用用户名和密码对其进行身份验证。

如果请求中没有附加任何参数,但在请求中有任何参数关联时失败,则可以正常工作。

请问我可以帮助我在请求中使用参数时如何验证URL?

import MultipartPostHandler 
import urllib2 

def handle_authentication(url): 
    """handle_authentication description""" 
    try: 
     passman = urllib2.HTTPPasswordMgrWithDefaultRealm() 
     passman.add_password(None, url, USERNAME, PASSWORD) 
     authhandler = urllib2.HTTPBasicAuthHandler(passman) 
     opener = urllib2.build_opener(authhandler) 
     urllib2.install_opener(opener) 
    except urllib2.HTTPError: 
     print ERROR_300 
     sys.exit(1) 

另一个功能:

params = {'BoardType': board_type, 
      'SataConfigFile': open(input_path, 'rb')} 
opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler) 
print opener 
try: 
    handle_authentication(url) 
    response = opener.open(url, params) 

这不验证URL - 你与你的任何解决方案?

回答

0

我不确定这是唯一的问题,但似乎你没有正确地组合开门人。您需要使用MultiPartPostHandler和HttpBasicAuthHandler作为参数来调用build_opener。

所以不是install_opener,试试这个:

import MultipartPostHandler 
import urllib2 

def build_authhandler(url): 
    passman = urllib2.HTTPPasswordMgrWithDefaultRealm() 
    passman.add_password(None, url, USERNAME, PASSWORD) 
    authhandler = urllib2.HTTPBasicAuthHandler(passman) 
    return urllib2.build_opener(authhandler) 

params = {'BoardType': board_type, 
      'SataConfigFile': open(input_path, 'rb')} 
auth_opener = build_authhandler(url) 
opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler, auth_opener) 

try:  
    response = opener.open(url, params) 
相关问题