Skip to content

Instantly share code, notes, and snippets.

@absent1706
Created April 8, 2016 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save absent1706/542c0b09ca574c0f6f873f345bdbaa90 to your computer and use it in GitHub Desktop.
Save absent1706/542c0b09ca574c0f6f873f345bdbaa90 to your computer and use it in GitHub Desktop.
python: debug urllib HTTPS request
# -*- coding: utf-8 -*-
import json, urllib2
#адрес для отправки json-запросов
url = 'https://api.direct.yandex.ru/v4/json/'
#данные для OAuth-авторизации
token = '6bfc962472504a2b99fb6a5b0b181d98'
#логин в Директе
login = 'igorxlib'
#структура входных данных (словарь)
data = {
'method': 'GetClientInfo',
'token': token,
'locale': 'ru',
'param': [login]
}
#конвертировать словарь в JSON-формат и перекодировать в UTF-8
jdata = json.dumps(data, ensure_ascii=False).encode('utf8')
h=urllib2.HTTPSHandler(debuglevel=1)
opener = urllib2.build_opener(h)
urllib2.install_opener(opener)
#выполнить запрос
response = urllib2.urlopen(url,jdata)
#вывести результат
print ''
print response.read().decode('utf8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment