forked from Jayhello/python_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_client_get.py
More file actions
64 lines (51 loc) · 1.92 KB
/
Copy pathhttp_client_get.py
File metadata and controls
64 lines (51 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# _*_ coding:utf-8 _*_
"""
This file a sample demo to do http stress test
"""
import requests
import time
from multiprocessing.dummy import Pool as ThreadPool
import urllib
def get_ret_from_http(url):
"""cited from https://stackoverflow.com/questions/645312/what-is-the-quickest-way-to-http-get-in-python
"""
ret = requests.get(url)
print ret.content
# eg. result: {"error":false,"resultMap":{"check_ret":1},"success":true}
def multi_process_stress_test():
"""
start up 4 thread to issue 1000 http requests to server
and test consume time
:return:
"""
start = time.time()
url = """http://127.0.0.1:9325/shortvideo/checkBlack?url=http%3A%2F%2Fzbasecapture.bs2.yy.com%2F42269159_1499248536403_3.jpg&serial=abcdddddddd"""
url1 = """http://127.0.0.1:9325/shortvideo/checkBlack?url=http%3A%2F%2Fgenie.bs2dl.yy.com%2Ff4955aa1ab1c479256e2a2c5cdec73a6&serial=abceeeeeeee"""
lst_url = [url, url1]*50
pool = ThreadPool(5)
ret = pool.map(get_ret_from_http, lst_url)
pool.close()
pool.join()
print 'time consume %s' % (time.time() - start)
def make_url():
"""
generate url with parameter
https://xy.com/index.php?url=http%3A//xy.xxx.com/22.jpg&SecretId=xy_123_move
cited from https://stackoverflow.com/questions/2506379/add-params-to-given-url-in-python
https://github.com/gruns/furl a good util for url operator
:return:
"""
para = {"SecretId": "xy_123_move", "url": "http://xy.xxx.com/22.jpg"}
print urllib.urlencode(para)
# url=http%3A%2F%2Fxy.xxx.com%2F22.jpg&SecretId=xy_123_move
base_url = 'xy.com/index.php'
# 记得 下面的是 ? 连接
return 'https://%s?%s' % (base_url, '&'.join('%s=%s' % (k, urllib.quote(str(v))) for k, v in para.iteritems()))
if __name__ == '__main__':
# get_ret_from_http()
# multi_process_stress_test()
print make_url()
# s = "abc"
s = "abc"
print urllib.quote(s)
pass