Jump to content
Sign in to follow this  
tschloss

Python: 'urlopen' seems to crash the script?

Recommended Posts

Hi, 

(among other starter problems) I have the following problem:

The below script runs fine in the Mac OSX terminal.

But as an "run script" action (type "python") it hangs. When I delete the urlopen line the script runs to the end (but of course without a JSON result).

The small workflow is here: http://d.pr/f/K3t

 

So something seems to be wrong with the line "response = urllib2....":

import urllib2
import urllib
import json

url='{query}'

endpoint='http://api.longurl.org/v2/expand'
params = {'url':url, 'title':'1','format':'json'}

response = urllib2.urlopen(endpoint+'?'+ urllib.urlencode(params))


try:
    json_data = json.loads(response.read())
    print 'URL: '+json_data[u'long-url']
    #print 'Page title: '+json_data[u'title']
except:
	print 'Sorry - could not find the long URL' 

How can I debug this?

How should I retrieve a JSON result from a webservice?

 

Thanks in advance!

Thomas

 

Share this post


Link to post

Sorry, guys - I am not sure why, but the workfow works no.

 

I have to find out how to catch an illegal URL - with a legal URL everythings works as expected

 

Thanks

Thomas

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  
×
×
  • Create New...