2

i have a custom url of the form

http://somekey:somemorekey@host.com/getthisfile.json

i tried all the way but getting errors :

method 1 :

from httplib2 import Http
ipdb> from urllib import urlencode
h=Http()
ipdb> resp, content = h.request("3b8138fedf8:1d697a75c7e50@abc.myshopify.com/admin/shop.json")

error :

No help on =Http()

Got this method from here

method 2 : import urllib

urllib.urlopen(url).read()

Error :

*** IOError: [Errno url error] unknown url type: '3b8108519e5378'

I guess something wrong with the encoding ..

i tried ...

ipdb> url.encode('idna')
*** UnicodeError: label empty or too long

Is there any way to make this Complex url get call easy .

Community
  • 1
  • 1
Ratan Kumar
  • 1,640
  • 3
  • 25
  • 52

2 Answers2

3

You are using a PDB-based debugger instead of a interactive Python prompt. h is a command in PDB. Use ! to prevent PDB from trying to interpret the line as a command:

!h = Http()

urllib requires that you pass it a fully qualified URL; your URL is lacking a scheme:

urllib.urlopen('http://' + url).read()

Your URL does not appear to use any international characters in the domain name, so you do not need to use IDNA encoding.

You may want to look into the 3rd-party requests library; it makes interacting with HTTP servers that much easier and straightforward:

import requests
r = requests.get('http://abc.myshopify.com/admin/shop.json', auth=("3b8138fedf8", "1d697a75c7e50"))
data = r.json()  # interpret the response as JSON data.
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

The current de facto HTTP library for Python is Requests.

import requests
response = requests.get(
  "http://abc.myshopify.com/admin/shop.json",
  auth=("3b8138fedf8", "1d697a75c7e50")
)
response.raise_for_status()  # Raise an exception if HTTP error occurs
print response.content  # Do something with the content.
AKX
  • 152,115
  • 15
  • 115
  • 172