我有一个链接,我想测试的稳健性,因为没有一个更好的词。我的代码可以多次按顺序传递URL:
# Testing for robustness
for i in range(100000):
city = 'New York'
city = '%20'.join(city.split(' '))
res = requests.get(f'http://example.com/twofishes?query={city}')
data = res.json()
geo = data['interpretations'][0]['feature']['geometry']['center']
print('pinging xtime: %s ' % str(i))
print(geo['lat'], geo['lng'])我想接受这个代码,但是点击链接说,10或12次一次。我不介意连续的敲击,但这不如一次敲打多次那么有效。我觉得这是一个快速的修改,for循环的出现和一个PULL函数的出现?
发布于 2018-11-27 15:26:32
下面是一个应该适用于此任务的示例程序。考虑到我不想被列入黑名单,我还没有对代码进行实际测试,看它是否有效。无论如何,它至少应该在你所寻找的东西的大致范围内。如果您希望同时执行所有线程,我将考虑添加事件。希望这能有所帮助。
码
import threading
import requests
import requests.exceptions as exceptions
def stress_test(s):
for i in range(100000):
try:
city = 'New York'
city = '%20'.join(city.split(' '))
res = s.get(f'http://example.com/twofishes?query={city}')
data = res.json()
geo = data['interpretations'][0]['feature']['geometry']['center']
print('pinging xtime: %s ' % str(i))
print(geo['lat'], geo['lng'])
except (exceptions.ConnectionError, exceptions.HTTPError, exceptions.Timeout):
pass
if __name__ == '__main__':
for i in range(1, 12):
s = requests.session()
t = threading.Thread(target=stress_test, args=(s,))
t.start()
for th in threading.enumerate():
if th != threading.current_thread():
th.join()https://stackoverflow.com/questions/53502380
复制相似问题