error reading api with urllib

simian336 at gmail.com simian336 at gmail.com
Tue Dec 15 20:54:46 EST 2015


Hi,

I am pretty new to python. I have a project reading an api with urllib. The problem is I have to sections of code almost exactly the same. The first url works great. They second one fails.

If I manually copy and paste the url in the browser ti works great.
The error I get back is...

Bad Request
b''
I have manipulated it a few ways and I sometimes she bad url type b'http

I have attempted to try to pass it as ascii and unicode but may not have been correct.

Thanks for any suggestions.

Simian




#Getting Individual Customer services Info
custinfostart='http://192.17.3.17:7780/dataservices/cdk?SP=md_cst.get_customer(%270003%27,%27'
custinforend ='%27,?,?int%20status)'
CustLink=custinfostart+curr_cust+custinforend
#print(CustLink)

req=urllib.request.Request(CustLink)
resp=urllib.request.urlopen(req)
respData=resp.read()
CustServ.write('Customer ID:'+curr_cust+'\n')
CustServ.write(str(respData)+'\n')

#*************************************************************************
#Getting Device Info
custinfostart='http://192.17.3.17:7780/dataservices/cdk?SP=md_dev.get_device(%270003%27,%272%27,%27'
custinforend ='%27,%27n%27,%271%27,%27100%27,?,?,?int status)'

#custinfostart="http://192.17.3.17:7780/dataservices/cdk?SP=md_dev.get_device('0003','2','"
#custinforend ="','n','1','100',?,?,?int status)"

print(custinfostart)
print(curr_cust)
print(custinforend)
CustLink=custinfostart+curr_cust+custinforend
#CustLink=CustLink.encode('ascii', 'ignore')
print('test')
print(CustLink)
     
######### Code Fails Here ##########

req=urllib.request.Request(CustLink)
#    resp=urllib.request.urlopen(req)
try: resp=urllib.request.urlopen(req)
except urllib.error.URLError as e:
    print(e.reason)
    respData=resp.read()
    print(respData)
    sys.exit("Device Error")

respData=resp.read()
DeviceInfo.write(str(respData)+'\n')



More information about the Python-list mailing list