使用threading多线程访问baidu,输出状态码及访问时间
import threading
import time
import requests
def baidu():
time_start = time.time()
r = requests.get(url='http://www.baidu.com')
use_time = time.time() - time_start
print('Status_Code:%s----%s----%s' % (r.status_code, use_time, time.strftime('%H:%M:%S')))
def main():
threads = []
threads_count = 10
for i in range(threads_count):
t = threading.Thread(target=baidu(), args=())
threads.append(t)
threads[i].start()
threads[i].join()
if __name__ == '__main__':
main()
执行结果: