2011-01-11 71 views
1

您好我能夠解析一個正常的xml,如xml = lxml.etree.parse(''http://abc.com/A.xml') 但現在我有這個路徑進行身份驗證用戶名和密碼可以輸入用戶名和密碼並解析url,就像連接數據庫一樣,您可以在連接字符串中輸入用戶名密碼在python中的安全xml解析

+1

-1。你拒絕接受答案,並且在被告知這樣做時你忽略了這個事實。學會回報。 – user225312 2011-01-11 16:41:17

回答

3

夥計們,我發現了一種解析密碼保護XML的方法,這就是我所做的。

import urllib2 
import base64 
theurl = 'http://abc.com/A.xml' 

username='AAA' 
password='BBB' 

req = urllib2.Request(theurl) 


base64string = base64.encodestring(
      '%s:%s' % (username, password))[:-1] 
authheader = "Basic %s" % base64string 
req.add_header("Authorization", authheader) 
try: 
    handle = urllib2.urlopen(req) 
except IOError, e: 
    print "It looks like the username or password is wrong." 
xml = handle.read() 
inputXml = etree.fromstring(xml) 
3

是的,這是可能的。在使用lxml解析XML文檔之前,您需要通過正確處理HTTP基本/摘要式身份驗證的HTTP請求來獲取它。例如,與urllib2.HTTPBasicAuthHandler像這樣的解決方案:Python urllib2 HTTPBasicAuthHandler