def visit_url(url, method): proto, domain, port, uri = split_url(url) # prepare for a request if proto == 'http': cli = http.Client(domain, port, timeout=20) if proto == 'https': context = ssl._create_unverified_context() cli = http.Client(domain, port, https_context=context, timeout=20) # send a request cli.request(uri, method=method, headers={}) # deal with response try: response_code = cli.status cli.request('/') reponse_msg = cli.headers except (socket.timeout, ssl.SSLError) as e: logger.warn('Socket timeout, ' + 'While access the {url}'.format(url=url)) return -1 except Exception as e: logger.exception(repr(e) + ' While access the {url}'.format(url=url)) return -1 return response_code, reponse_msg
def _req(self, req): if 'headers' not in req: req['headers'] = { 'host': '{ip}:{port}'.format(ip=self.ip, port=self.port), } elif 'host' not in req['headers']: req['headers']['host'] = '{ip}:{port}'.format(ip=self.ip, port=self.port) body = req.get('body', None) if body is not None: req['headers']['Content-Length'] = len(body) self._sign_req(req) cli = http.Client(self.ip, self.port, self.timeout) cli.send_request(req['uri'], method=req['verb'], headers=req['headers']) if body is not None: cli.send_body(req['body']) cli.read_response() res = cli.read_body(None) msg = 'Status:{s} req:{req} res:{res} server:{ip}:{p}'.format( s=cli.status, req=repr(req), res=repr(res), ip=self.ip, p=self.port) if cli.status == 404: raise KeyNotFoundError(msg) elif cli.status != 200: raise ServerResponseError(msg) return res
def send_post_request(host, port, headers, fields): conn = http.Client(host, port) multipart_cli = httpmultipart.Multipart() headers = multipart_cli.make_headers(fields, headers) body = multipart_cli.make_body_reader(fields) conn.send_request('/', method='POST', headers=headers) for data in body: conn.send_body(data) ret_status, ret_headers = conn.read_response() body = [] while True: buf = conn.read_body(1024*1024) if buf == '': break body.append(buf) return { 'status_code': ret_status, 'headers': ret_headers, 'body': ''.join(body), }
def _request_one_ip(self, ip, request, timeout): request_copy = copy.deepcopy(request) request_copy['headers']['Host'] = ip if self.signer is not None: self.signer.add_auth(request_copy, sign_payload=True) h = http.Client(ip, self.port, timeout=timeout) h.send_request(request_copy['uri'], request_copy['verb'], request_copy['headers']) h.send_body(request_copy['body']) resp_status, resp_headers = h.read_response() bufs = [] while True: buf = h.read_body(1024 * 1024 * 100) if buf == '': break bufs.append(buf) resp_body = ''.join(bufs) return resp_status, resp_body, resp_headers
def delete_file(bucket, key): idx = random.randint(1,10000)%len(hosts) h = http.Client(hosts[idx], 80) h.request('/'+bucket+'/'+key, 'DELETE') if h.status != 204: raise DeleteError( 'Delete File Code: ' + str(h.status) )
def get_file(bucket, key): idx = random.randint(1, 1000)%len(hosts) h = http.Client(hosts[idx], 80) h.request('/'+bucket+'/'+key, 'GET') if h.status != 200: raise GetError('GET File Code: ' + str(h.status)) h.read_body(50*1024*1024)
def upload_file(bucket, key): idx = random.randint(1, 10000)%len(hosts) h = http.Client(hosts[idx], 80) h.send_request('/'+bucket+'/'+key, 'PUT', headers = {'x-amz-acl': 'public-read', 'Content-Length': len(data), 'Host': 's2.i.qingcdn.com'} ) h.send_body(data) h.read_response() if h.status != 200: raise UploadError( 'Put File Code: ' + str(h.status) + h.read_body(1024 * 1024) )
def _request(self, url, method, params, timeout, bodyinjson): while True: host, port, path = self._parse_url(url) if host is None or port is None or path is None: raise EtcdException('url is invalid, {url}'.format(url=url)) qs = {} headers = {} body = '' if method in (self._MGET, self._MDELETE): qs.update(params or {}) # use once, coz params is in location's query string params = None headers['Content-Length'] = 0 elif method in (self._MPUT, self._MPOST): if bodyinjson: if params is not None: body = utfjson.dump(params) headers.update({ 'Content-Type': 'application/json', 'Content-Length': len(body) }) else: body = urllib.urlencode(params or {}) headers.update({ 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': len(body) }) else: raise EtcdRequestError('HTTP method {method} not supported' ''.format(method=method)) if len(qs) > 0: if '?' in path: path = path + '&' + urllib.urlencode(qs) else: path = path + '?' + urllib.urlencode(qs) if self.basic_auth_account is not None: auth = { 'Authorization': 'Basic {ant}'.format( ant=self.basic_auth_account.encode('base64').strip()), } headers.update(auth) logger.debug('connect -> {mtd} {url}{path} {timeout}'.format( mtd=method, url=self._base_uri, path=path, timeout=timeout)) h = http.Client(host, port, timeout) h.send_request(path, method, headers) h.send_body(body) h.read_response() resp = Response.from_http(h) if not self.allow_redirect: return resp if resp.status not in Response.REDIRECT_STATUSES: return resp url = resp.get_redirect_location() if url is None: raise EtcdResponseError('location not found in {header}' ''.format(header=resp.headers)) logger.debug('redirect -> ' + url)
{ 'bucket': bucket_name, }, ], }, }, 'sign_args': { 'access_key': access_key, 'secret_key': secret_key, 'request_date': '20180918T120101Z', } } signed_post = Request(post_case, body=open('./test.txt')) conn = http.Client(host, port) conn.send_request(signed_post['uri'], method=signed_post['verb'], headers=signed_post['headers']) # signed_post['body'] is a generator object for body in signed_post['body']: conn.send_body(body) print conn.read_response() # send a put request file_content = "test sending put request" put_case = { 'verb': 'PUT', 'uri': '/bucket_name/key_name', 'args': { 'foo2': 'bar2', 'foo1': True, 'foo3': ['bar3', True],
from pykit import http if __name__ == '__main__': headers = { 'Host': '127.0.0.1', 'Accept-Language': 'en, mi', } h_client = http.Client('127.0.0.1', 8000) h_client.send_request('./test.txt', method='GET', headers=headers) test_status = h_client.read_status() test_headers = h_client.read_headers() test_boby = h_client.read_body(1024) test_trace = h_client.get_trace_str() print test_status print test_headers print test_boby print test_trace print test = 'wo shi cheng yang ' headers['content-length'] = len(test) h_client.send_request('./test.txt', method='POST', headers=headers) h_client.send_body(test) test_status, test_headers = h_client.read_response()