首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python:一次多次敲击URL以进行测试

Python:一次多次敲击URL以进行测试
EN

Stack Overflow用户
提问于 2018-11-27 14:56:29
回答 1查看 351关注 0票数 0

我有一个链接,我想测试的稳健性,因为没有一个更好的词。我的代码可以多次按顺序传递URL:

代码语言:javascript
复制
# 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函数的出现?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-27 15:26:32

下面是一个应该适用于此任务的示例程序。考虑到我不想被列入黑名单,我还没有对代码进行实际测试,看它是否有效。无论如何,它至少应该在你所寻找的东西的大致范围内。如果您希望同时执行所有线程,我将考虑添加事件。希望这能有所帮助。

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53502380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档