client-barcode: Fix random 401s with HTTP authentication

This commit is contained in:
Sebastian Pipping 2011-10-05 17:30:05 +02:00
parent 3402393fe4
commit 11122966b5
1 changed files with 6 additions and 11 deletions

View File

@ -10,6 +10,7 @@ import urllib2
import simplejson as json
from decimal import Decimal
import os
import base64
if __name__ == '__main__':
import sys
@ -24,14 +25,8 @@ DEPOSIT_BANK = 2
_BASE_URL = 'http://devcat.someserver.de:13805/api2/'
def _make_auth_opener(username, password):
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, _BASE_URL, username, password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
return urllib2.build_opener(handler)
_auth_opener = _make_auth_opener(os.environ['BARCODE_PLUGIN_USER'], os.environ['BARCODE_PLUGIN_PASS'])
_auth = base64.encodestring('%s:%s' % (os.environ['BARCODE_PLUGIN_USER'], os.environ['BARCODE_PLUGIN_PASS'])).rstrip()
_headers = {'Authorization':'Basic %s' % _auth}
def _request(api_rel_url, para_map, method):
@ -40,13 +35,13 @@ def _request(api_rel_url, para_map, method):
if method == 'GET':
url = '%s?%s' % (url, data)
req = urllib2.Request(url)
req = urllib2.Request(url, headers=_headers)
elif method == 'POST':
req = urllib2.Request(url, data)
req = urllib2.Request(url, data=data, headers=_headers)
else:
raise ValueError('Unsupported method "%s"' % method)
response = _auth_opener.open(req)
response = urllib2.urlopen(req)
return response.read()