Jump to content

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

 

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...