def getpage(url, params="", unicode_mode=False, headers=False): # Importación dentro de función, para no causar referencia circular. from httpclient import HTTPClient # Encodeo de parametro 'search'. url += urlencode(params) # Agregación de http:// en caso que sea necesario if not urlparse.urlparse(url).scheme: url = "http:" + url client = HTTPClient() # Creación de request. http = client.open(url) # Si se requiere en formato unicode if unicode_mode: # Si se requieren encabezados if headers: return (unicode(http.read(), "UTF-8"), http.headers) else: return unicode(http.read(), "UTF-8") else: if headers: return (http.read(), http.headers) else: return http.read()
def test_base(self): client = HTTPClient() self.assertTrue(client) self.assertEqual(client.agent, "python-httpclient/{v}".format(v=__version__)) client = HTTPClient(agent='foo') self.assertEqual(client.agent, 'foo')
def test_GET(self): client = HTTPClient(url="http://httpbin.org/get", requests=webtest.TestApp(Application())) response = client.GET() assert '"Host": "httpbin.org"' in response assert '"args": {}' in response
def test_follow(self): client = HTTPClient(url="http://httpbin.org/anything") assert client._url == "http://httpbin.org/anything" client2 = client.follow("me") assert client2._url == "http://httpbin.org/anything/me"
def test_DELETE(self): client = HTTPClient(url="http://httpbin.org/anything/27", requests=webtest.TestApp(Application())) response = client.DELETE() assert '"method": "DELETE"' in response assert '"url": "http://httpbin.org/anything/27"' in response
def test_GET(self): client = HTTPClient(url="http://httpbin.org/get") with requests_mock.Mocker() as m: m.get(client._url, text='{"Host": "httpbin.org", "args": {}}') response = client.GET() assert '"Host": "httpbin.org"' in response assert '"args": {}' in response
def test_GET_params(self): client = HTTPClient(url="http://httpbin.org/get?alpha=1", requests=webtest.TestApp(Application())) response = client.GET() response = json.loads(response) assert response["headers"]["Host"] == "httpbin.org" assert response["args"] == {"alpha": "1"}
def test_POST(self): client = HTTPClient(url="http://httpbin.org/post?alpha=1", requests=webtest.TestApp(Application())) response = client.POST(beta=2) response = json.loads(response) assert response["headers"]["Host"] == "httpbin.org" assert response["args"] == {"alpha": "1"} assert response["form"] == {"beta": "2"}
def test_GET_params(self): client = HTTPClient(url="http://httpbin.org/get?alpha=1") with requests_mock.Mocker() as m: m.get(client._url, text='''{"headers": {"Host": "httpbin.org"}, "args": {"alpha": "1"}}''') response = client.GET() response = json.loads(response) assert response["headers"]["Host"] == "httpbin.org" assert response["args"] == {"alpha": "1"}
def test_DELETE(self): client = HTTPClient(url="http://httpbin.org/anything/27") with requests_mock.Mocker() as m: m.delete(client._url, json={ "method": "DELETE", "url": "http://httpbin.org/anything/27" }) response = client.DELETE() assert '"method": "DELETE"' in response assert '"url": "http://httpbin.org/anything/27"' in response
def _test_request(self, method): client = HTTPClient() client.add_handler('request_send', _test_cb) request = Request(method, tests[method]['url'], tests[method]['headers']) if 'content' in tests[method]: request.content = tests[method]['content'] resp = client.request(request) self.assertTrue(resp) self.assertEqual(resp.status, 204)
def test_POST(self): client = HTTPClient(url="http://httpbin.org/post?alpha=1") with requests_mock.Mocker() as m: m.post(client._url, json={"headers": {"Host": "httpbin.org"}, "args": {"alpha": "1"}, "form": {"beta": "2"}}) response = client.POST(beta=2) response = json.loads(response) assert response["headers"]["Host"] == "httpbin.org" assert response["args"] == {"alpha": "1"} assert response["form"] == {"beta": "2"}
def test_prepare(self): request = Request('FOO', 'http') client = HTTPClient() client.add_handler('request_prepare', _cb_request_prepare) resp = client.request(request) self.assertEqual(resp.status, 400) handlers = Handlers() handlers.add_handler('request_prepare', _cb_request_prepare_change_request) req = handlers.dispatch('request_prepare', request) self.assertEqual(req.method, 'PUT') handlers = Handlers() handlers.add_handler('request_prepare', _cb_request_prepare_do_nothing) req = handlers.dispatch('request_prepare', request) self.assertEqual(req.method, 'FOO') self.assertEqual(req.url, 'http')
def __init__(self): self.http_client = HTTPClient(None) self.ssl = False self.port = 80 self.server = 'picasaweb.google.com' self.additional_headers = {} self.additional_headers['GData-Version'] = '2' # self.additional_headers['User-Agent'] = atom.http_interface.USER_AGENT % ( # application_name,) self.token = None
def test_decorator_exception(self): client = HTTPClient(with_exceptions=True) client.add_handler('request_send', _exception_cb) with self.assertRaises(Exception) as cm: client.get('http://lumberjaph.net') exception = cm.exception self.assertTrue(exception) self.assertTrue(exception.is_client_error) self.assertEqual(str(exception), "Not Found") # without `with_exception`, no exception should be raised client = HTTPClient() client.add_handler('request_send', _exception_cb) res = client.get('http://lumberjaph.net') self.assertEqual(res.status, 404)
def __init__(self, esmcfg, api_version='2'): """Initialize the ESM class. Args: esmcfg: Instance of ESMConfig object OR Dict with these keys: {'esmhost': '<ESM-IP>', 'esmuser': <ESM-USERNAME>, 'esmpass': <ESM-PASSWORD>} api_version (str): '1' or '2'. Defaults to '2'. Returns: Instance of ESM Raises: ValueError: esmcfg required. ValueError: esmhost, esmuser, esmpass required in esmcfg. ValueError: api_version must be '1' or '2'. """ if api_version is not '2': # or not '2': raise ValueError('api_version is "1" or "2".') try: esmuser = esmcfg['esmuser'] except KeyError: raise ValueError('Invalid esmcreds: Missing esmuser.') try: esmpass = esmcfg['esmpass'] except KeyError: raise ValueError('Invalid esmcfg: Missing esmpass.') try: esmhost = esmcfg['esmhost'] except KeyError: raise ValueError('Invalid esmcfg: Missing esmhost.') self.session = HTTPClient() self.base_url = self._set_base_url(esmhost, api_version) self.int_url = 'https://{}/ess'.format(esmhost) self.login_url = 'https://{}/rs/esm/login'.format(esmhost) self._setup_auth(esmuser, esmpass) self.logged_in = False
class Service: def __init__(self): self.http_client = HTTPClient(None) self.ssl = False self.port = 80 self.server = 'picasaweb.google.com' self.additional_headers = {} self.additional_headers['GData-Version'] = '2' # self.additional_headers['User-Agent'] = atom.http_interface.USER_AGENT % ( # application_name,) self.token = None def authenticate(self, email, password, num_tried=0): auth_request_url = AUTH_SERVER_HOST + '/accounts/ClientLogin' request_fields = {'Email': email, 'Passwd': password, 'accountType': 'HOSTED_OR_GOOGLE', 'service': 'lh2', 'source': None} request_body = urllib.urlencode(request_fields) try: auth_response = self.http_client.request(auth_request_url, data=request_body) except urllib2.HTTPError, e: response_body = e.read() if e.code == 403: if response_body.splitlines()[0] == 'Error=BadAuthentication': raise BadAuthentication, 'Incorrect username or password' else: raise Exception, 'Server responded with a 403 code. \ Message:\n' + response_body else: raise BadAuthentication, 'Authentication process failed. \ Message:\n' + response_body except urllib2.URLError, e: if num_tried < 5: #log(self.__class__, "login attempt: %d failed - %s" % #(num_tried + 1, str(e.reason))) time.sleep(3) return self.authenticate(email, password, num_tried + 1) raise BadAuthentication, 'Authentication process failed. \ Message:\n' + str(e.reason)
def test_response(self): request = Request('GET', 'http') client = HTTPClient() client.add_handler('request_send', _cb_request_send) #client.add_handler('response_done', _cb_response_done) resp = client.request(request)
def new_from_url(spec_url, base_url=None): http_client = HTTPClient() response = http_client.get(spec_url) return new_from_string(response.content, base_url)
from http import Request from httpclient import HTTPClient _client = HTTPClient() def client(custom_client=None): global _client if custom_client is None: return _client _client = custom_client return client def get(url): result = client().get(url) return result def head(url): req = Request('HEAD', url) result = client().request(req) if result.is_success: return result return None def mirror(url, filename): client().mirror(url, filename)
class ESM(object): def __init__(self, esmcfg, api_version='2'): """Initialize the ESM class. Args: esmcfg: Instance of ESMConfig object OR Dict with these keys: {'esmhost': '<ESM-IP>', 'esmuser': <ESM-USERNAME>, 'esmpass': <ESM-PASSWORD>} api_version (str): '1' or '2'. Defaults to '2'. Returns: Instance of ESM Raises: ValueError: esmcfg required. ValueError: esmhost, esmuser, esmpass required in esmcfg. ValueError: api_version must be '1' or '2'. """ if api_version is not '2': # or not '2': raise ValueError('api_version is "1" or "2".') try: esmuser = esmcfg['esmuser'] except KeyError: raise ValueError('Invalid esmcreds: Missing esmuser.') try: esmpass = esmcfg['esmpass'] except KeyError: raise ValueError('Invalid esmcfg: Missing esmpass.') try: esmhost = esmcfg['esmhost'] except KeyError: raise ValueError('Invalid esmcfg: Missing esmhost.') self.session = HTTPClient() self.base_url = self._set_base_url(esmhost, api_version) self.int_url = 'https://{}/ess'.format(esmhost) self.login_url = 'https://{}/rs/esm/login'.format(esmhost) self._setup_auth(esmuser, esmpass) self.logged_in = False def _set_base_url(self, host, api_version): """ESM public URL setter""" if str(api_version) == '2': return 'https://{}/rs/esm/v2/'.format(host) elif str(api_version) == '1': return 'https://{}/rs/esm/'.format(host) else: raise ValueError('API Version must be 1 or 2.') def _setup_auth(self, user, passwd): """Interface to auth setup functions""" self._auth_data = {} self._auth_data['username'] = base64.b64encode( user.encode('utf-8')).decode() self._auth_data['password'] = base64.b64encode( passwd.encode('utf-8')).decode() self._auth_data['locale'] = 'en_US' self._auth_data['os'] = 'Win32' def login(self): method = 'login' cb = self._set_header self.post(method, data=self._auth_data, callback=cb, raw=True) def logout(self): """ """ method = 'logout' url = self._set_url(method) self.session.delete(url) def _set_header(self, resp): """Adds field to http header. Args: Requests response object. """ self.session.add_header( {'X-Xsrf-Token': resp.headers.get('Xsrf-Token')}) self.logged_in = True def _set_url(self, method): if method.isupper(): url = self._int_url data = self._format_priv_params(method, **data) elif method is 'login': url = self.login_url else: url = ''.join([self.base_url, method]) return url def post(self, method, data=None, callback=None, raw=False): url = self._set_url(method) resp = self.session.post(url, data=data) if raw: data = resp else: data = self._unpack_resp(resp) if callback: return callback(data) return data def _unpack_resp(self, response): """Unpack data from response. Args: response: requests response object """ data = None try: if isinstance(response.json(), list): data = response.json() elif isinstance(response.json(), dict): try: data = response.json()['value'] except KeyError: try: response.json()['return'] except KeyError: data = response.json() return data except json.decoder.JSONDecodeError: return def watchlist_fields(self): method = 'sysGetWatchlistFields' return self.post(method) def watchlist_summary(self): method = 'sysGetWatchlists?hidden=false&dynamic=false&writeOnly=false&indexedOnly=false' return self.post(method) def get_watchlist_details(self, w_id): method = 'sysGetWatchlistDetails' data = {'id': w_id} return self.post(method, data=data) def export_watchlist(self, w_id): wl_data = [] file_size = 0 bytes_read = 0 wl_details = self.get_watchlist_details(w_id) filename = wl_details.get('valueFile').get('fileToken') file_data = self._get_watchlist_vals(filename, bytes_read) wl_data.append(file_data.get('data')) bytes_read += file_data.get('bytesRead') while bytes_read != file_data.get('fileSize'): file_data = self._get_watchlist_vals(filename, bytes_read) bytes_read += file_data.get('bytesRead') wl_data.append(file_data.get('data')) return wl_data def _get_watchlist_vals(self, w_id, byte_pos): method = 'sysGetWatchlistValues?pos={}&count=0'.format(byte_pos) data = {"file": {"id": w_id}} return self.post(method, data=data)
def testName(self): ua = HTTPClient() ua.add_handler('request_send', _cb) client = GitHubClient(user_agent=ua) res = client.get_user('franckcuny') self.assertEqual(res['name'], 'batman')
def _get_client(self): client = HTTPClient() return client
def setUp(self): self.url = "http://lumberjaph.net" custom_client = HTTPClient() custom_client.add_handler("request_send", _cb) client(custom_client)
def __init__(self, user_agent=None, base_url='https://api.github.com'): self.base_url = base_url if user_agent is None: self._user_agent = HTTPClient() else: self._user_agent = user_agent
def test_headers(self): client = HTTPClient() self.assertTrue(client.default_headers.get('Connection')) self.assertEqual(client.default_headers.get('User-Agent'), 'python-httpclient/{v}'.format(v=__version__))
def a(): client = HTTPClient() response = client.command('127.0.0.1:8080/', command='GET') print('Code: {0}'.format(response.code)) print('Body: {0}'.format(response.body))