Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100487 > unrolled thread

error reading api with urllib

Started bysimian336@gmail.com
First post2015-12-15 17:54 -0800
Last post2015-12-17 11:29 -0800
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  error reading api with urllib simian336@gmail.com - 2015-12-15 17:54 -0800
    Re: error reading api with urllib Simian <simian336@gmail.com> - 2015-12-15 18:46 -0800
      Re: error reading api with urllib Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-16 16:22 -0700
    Re: error reading api with urllib John Gordon <gordon@panix.com> - 2015-12-16 22:12 +0000
      Re: error reading api with urllib Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-16 16:24 -0700
    Re: error reading api with urllib Simian <simian336@gmail.com> - 2015-12-17 11:29 -0800

#100487 — error reading api with urllib

Fromsimian336@gmail.com
Date2015-12-15 17:54 -0800
Subjecterror reading api with urllib
Message-ID<9aa21642-765b-4666-8c66-a6dab9928c37@googlegroups.com>
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')

[toc] | [next] | [standalone]


#100489

FromSimian <simian336@gmail.com>
Date2015-12-15 18:46 -0800
Message-ID<f9370000-5b40-41e5-a355-471f3225c6ef@googlegroups.com>
In reply to#100487
I added 

except urllib.error.HTTPError as e:
     print('HTTP Errpr')
     print('Error code: ', e.code)

to my try and I recieve...

400: ('Bad Request',
     'Bad request syntax or unsupported method'),

but processing the string with a browser works fine.

Simi

[toc] | [prev] | [next] | [standalone]


#100540

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-12-16 16:22 -0700
Message-ID<mailman.25.1450308211.30845.python-list@python.org>
In reply to#100489
On Tue, Dec 15, 2015 at 7:46 PM, Simian <simian336@gmail.com> wrote:
> I added
>
> except urllib.error.HTTPError as e:
>      print('HTTP Errpr')
>      print('Error code: ', e.code)
>
> to my try and I recieve...
>
> 400: ('Bad Request',
>      'Bad request syntax or unsupported method'),
>
> but processing the string with a browser works fine.

Have you tried requesting the same URL with curl?

[toc] | [prev] | [next] | [standalone]


#100538

FromJohn Gordon <gordon@panix.com>
Date2015-12-16 22:12 +0000
Message-ID<n4snji$mi4$1@reader1.panix.com>
In reply to#100487
In <9aa21642-765b-4666-8c66-a6dab9928c37@googlegroups.com> simian336@gmail.com writes:

> Bad Request
> b''


That probably means you aren't using one of the recognized methods
(i.e. GET, POST, etc.)

It doesn't look like you are specifying one of these methods on your
Request object.  Try doing that.

(It works in your browser because it defaults to GET automatically.)

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#100541

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-12-16 16:24 -0700
Message-ID<mailman.26.1450308337.30845.python-list@python.org>
In reply to#100538
On Wed, Dec 16, 2015 at 3:12 PM, John Gordon <gordon@panix.com> wrote:
> In <9aa21642-765b-4666-8c66-a6dab9928c37@googlegroups.com> simian336@gmail.com writes:
>
>> Bad Request
>> b''
>
>
> That probably means you aren't using one of the recognized methods
> (i.e. GET, POST, etc.)
>
> It doesn't look like you are specifying one of these methods on your
> Request object.  Try doing that.
>
> (It works in your browser because it defaults to GET automatically.)

urllib.request.Request also defaults to GET unless the request
includes data, in which case it defaults to POST. I would be more
inclined to suspect a problem with the stored procedure being called.

[toc] | [prev] | [next] | [standalone]


#100571

FromSimian <simian336@gmail.com>
Date2015-12-17 11:29 -0800
Message-ID<8a7a75fc-d023-4f83-ab78-2f30d286ad7d@googlegroups.com>
In reply to#100487
I will try adding the get.

I have not used curl.

I also forgot to mention that the code runs against another server, though a slightly different version number.

Thanks to you both.

Simian

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web