Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def postRequest(url, files=[], fields=[]):
content_type, body = encode_multipart_formdata(fields=fields, files=files)
headers = {'Content-Type': content_type, 'Accept': '*/*'}
url = URL(url)
http = HTTPClient.from_url(url)
response = http.request('POST', url.request_uri, body=body, headers=headers)
return response
def get(path="/sub/channel"):
url = URL("http://%s/%s" % (os.getenv("HTTP_HOST", 'localhost'), path))
http = HTTPClient.from_url(url)
response = http.get(path)
return response
def postRequest(url, files=[], fields=[]):
content_type, body = encode_multipart_formdata(fields=fields, files=files)
headers = {'Content-Type': content_type, 'Accept': '*/*'}
url = URL(url)
http = HTTPClient.from_url(url)
response = http.request('POST', url.request_uri, body=body, headers=headers)
return response
def get(path="/sub/channel"):
url = URL("http://%s/%s" % (os.getenv("HTTP_HOST", 'localhost'), path))
http = HTTPClient.from_url(url)
response = http.get(path)
return response
def swift_auth_v2(self):
self.tenant, self.user = self.user.split(';')
auth_dict = {}
auth_dict['auth'] = {'passwordCredentials':
{
'username': self.user,
'password': self.password,
},
'tenantName': self.tenant}
auth_json = json_dumps(auth_dict)
headers = {'Content-Type': 'application/json'}
auth_httpclient = HTTPClient.from_url(
self.auth_url,
connection_timeout=self.http_timeout,
network_timeout=self.http_timeout,
)
path = urlparse.urlparse(self.auth_url).path
if not path.endswith('tokens'):
path = posixpath.join(path, 'tokens')
ret = auth_httpclient.request('POST', path,
body=auth_json,
headers=headers)
if ret.status_code < 200 or ret.status_code >= 300:
raise SwiftException('AUTH v2.0 request failed on ' +
'%s with error code %s (%s)'
% (str(auth_httpclient.get_base_url()) +
path, ret.status_code,
def swift_auth_v2(self):
self.tenant, self.user = self.user.split(';')
auth_dict = {}
auth_dict['auth'] = {'passwordCredentials':
{
'username': self.user,
'password': self.password,
},
'tenantName': self.tenant}
auth_json = json_dumps(auth_dict)
headers = {'Content-Type': 'application/json'}
auth_httpclient = HTTPClient.from_url(
self.auth_url,
connection_timeout=self.http_timeout,
network_timeout=self.http_timeout,
)
path = urlparse.urlparse(self.auth_url).path
if not path.endswith('tokens'):
path = posixpath.join(path, 'tokens')
ret = auth_httpclient.request('POST', path,
body=auth_json,
headers=headers)
if ret.status_code < 200 or ret.status_code >= 300:
raise SwiftException('AUTH v2.0 request failed on ' +
'%s with error code %s (%s)'
% (str(auth_httpclient.get_base_url()) +
path, ret.status_code,
if __name__ == "__main__":
N = 1000
C = 10
url = URL('http://127.0.0.1/index.html')
qs = url.request_uri
def run(client):
response = client.get(qs)
response.read()
assert response.status_code == 200
client = HTTPClient.from_url(url, concurrency=C)
group = gevent.pool.Pool(size=C)
now = time.time()
for _ in xrange(N):
group.spawn(run, client)
group.join()
delta = time.time() - now
req_per_sec = N / delta
print "request count:%d, concurrenry:%d, %f req/s" % (
N, C, req_per_sec)
def swift_auth_v1(self):
self.user = self.user.replace(";", ":")
auth_httpclient = HTTPClient.from_url(
self.auth_url,
connection_timeout=self.http_timeout,
network_timeout=self.http_timeout,
)
headers = {'X-Auth-User': self.user,
'X-Auth-Key': self.password}
path = urlparse.urlparse(self.auth_url).path
ret = auth_httpclient.request('GET', path, headers=headers)
# Should do something with redirections (301 in my case)
if ret.status_code < 200 or ret.status_code >= 300:
raise SwiftException('AUTH v1.0 request failed on ' +
'%s with error code %s (%s)'
% (str(auth_httpclient.get_base_url()) +
self.conf.getint('swift', 'http_pool_length') or 10
self.region_name = self.conf.get("swift", "region_name") or "RegionOne"
self.endpoint_type = \
self.conf.get("swift", "endpoint_type") or "internalURL"
self.cache_length = self.conf.getint("swift", "cache_length") or 20
self.chunk_length = self.conf.getint("swift", "chunk_length") or 12228
self.root = root
block_size = 1024 * 12 # 12KB
if self.auth_ver == "1":
self.storage_url, self.token = self.swift_auth_v1()
else:
self.storage_url, self.token = self.swift_auth_v2()
token_header = {'X-Auth-Token': str(self.token)}
self.httpclient = \
HTTPClient.from_url(str(self.storage_url),
concurrency=self.http_pool_length,
block_size=block_size,
connection_timeout=self.http_timeout,
network_timeout=self.http_timeout,
headers=token_header)
self.base_path = str(posixpath.join(
urlparse.urlparse(self.storage_url).path, self.root))
'oauth_consumer_key': oauthlib_consumer.key,
'locations': '-122.75,36.8,-121.75,37.8' # San Francisco
}
url = URL('https://stream.twitter.com/1/statuses/filter.json')
req = oauthlib.Request.from_consumer_and_token(
oauthlib_consumer,
token=token,
http_url=str(url),
http_method='POST')
signature_method = oauthlib.SignatureMethod_HMAC_SHA1()
req = oauthlib.Request(method="POST", url=str(url), parameters=params)
req.sign_request(signature_method, oauthlib_consumer, token)
http = HTTPClient.from_url(url)
response = http.request('POST', url.request_uri,
body=req.to_postdata(),
headers={'Content-Type': 'application/x-www-form-urlencoded',
'Accept': '*/*'})
data = json.loads(response.readline())
while data:
pp(data)
data = json.loads(response.readline())