Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

download webpage api hangs

Status
Not open for further replies.

simian336

Programmer
Sep 16, 2009
723
US
I am trying to download a web page from a local program accessed thru an api. It works thru a browser and it works in python but it appears to hang and just sits there like the request never closes. It actually prints the contents to shell.

Any ideas?

Simi


import urllib.request

link=""" status)"""
print(link)

x = urllib.request.urlopen(link)
print(x.read())
 
Also a similar string is processed to open the connection and it opens it and returns in less then a second.

Simi
 
This appears to fix the problem. It is not super fast but it takes are of the problem.

response = urllib.request.urlopen(link, timeout=.5).read().decode('utf-8')

It still takes 5 seconds to retrieve 3000 rows.

Thanks

Simi
 
HTTP connections do not 'close' themselves. Because HTTP is a 'disconnected protocol', a http user agent should read the amount of bytes that the server reports it is going to send (sec 14.13 Content-Length) from the HTTP: Response Header, then disconnect itself when that number has been received.


Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Here are 2 nearly identical pieces of code. the first call rund fine, the second one fails. But if I have it print the url and I paste the second one in the browser it runs fine?

Any ideas?

Thanks Simi


Code:
cust='[URL unfurl="true"]http://192.17.3.17:7780/dataservices/cdk?SP=md_cst.get_customer_list(%270003%27,%271%27,%275000%27,%271%27,%27%27,?,?,?int%20status)'[/URL]
print(cust)
req=urllib.request.Request(cust)
resp=urllib.request.urlopen(req)
respData=resp.read()
print(respData)

#########  This fails  ###############################
cust="[URL unfurl="true"]http://192.17.3.17:7780/dataservices/cdk?SP=md_dev.get_device_list('0003','2','','n','1','1000',?,?,?int[/URL] status)"
print(cust)
req=urllib.request.Request(cust)
resp=urllib.request.urlopen(req)
respData=resp.read()
print(respData)
 
Probably because the second one is not URL encoded and the single quotes 'break' the receiving end.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
I have tried it both ways and get the same result.

Here is the latest error messages.

Traceback (most recent call last):
File "C:\Program Files\python33\url_test.py", line 32, in <module>
resp=urllib.request.urlopen(req)
File "C:\Program Files\python33\lib\urllib\request.py", line 160, in urlopen
return opener.open(url, data, timeout)
File "C:\Program Files\python33\lib\urllib\request.py", line 479, in open
response = meth(req, response)
File "C:\Program Files\python33\lib\urllib\request.py", line 591, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Program Files\python33\lib\urllib\request.py", line 517, in error
return self._call_chain(*args)
File "C:\Program Files\python33\lib\urllib\request.py", line 451, in _call_chain
result = func(*args)
File "C:\Program Files\python33\lib\urllib\request.py", line 599, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

Thanks

Simi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top