zoukankan      html  css  js  c++  java
  • Caused by SSLError

     1 import requests
     2 from bs4 import BeautifulSoup
     3 
     4 
     5 def get_page():
     6     url = 'https://movie.douban.com/cinema/nowplaying/changsha/'
     7     headers = {
     8         "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
     9     }
    10     response = requests.get(url, headers=headers)
    11     text = response.text
    12     return text
    13 
    29 
    30 if __name__ == '__main__':
    31     text = get_page()
    32

    程序运行报错:requests.exceptions.SSLError: HTTPSConnectionPool(host='movie.douban.com', port=443): Max retries exceeded with url: /cinema/nowplaying/changsha/ (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)'),))

    其实很奇怪,我昨天晚上的时候代码还正常运行了,今天竟然就报错了,百度找到一个解决方法,原文内容:

    python locust接口性能测试HTTPS网站报错:Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certi

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/rziqq/article/details/78795289

    问题描述:

    测试HTTPS SSL 协议的网站接口,用Python Locust模块,不论POST还是GET都提示错误:

    SSLError
    Max retries exceeded with url: /action.php?m=upload 
    (Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)'),))",),)

    后面查官网locust.io,终于找到解决方法:https://docs.locust.io/en/latest/api.html

    request(method, url, name=None, catch_response=False, **kwargs)

    Constructs and sends a requests.Request. Returns requests.Response object.

    Parameters:
    • method – method for the new Request object.
    • url – URL for the new Request object.
    • name – (optional) An argument that can be specified to use as label in Locust’s statistics instead of the URL path. This can be used to group different URL’s that are requested into a single entry in Locust’s statistics.
    • catch_response – (optional) Boolean argument that, if set, can be used to make a request return a context manager to work as argument to a with statement. This will allow the request to be marked as a fail based on the content of the response, even if the response code is ok (2xx). The opposite also works, one can use catch_response to catch a request and then mark it as successful even if the response code was not (i.e 500 or 404).
    • params – (optional) Dictionary or bytes to be sent in the query string for the Request.
    • data – (optional) Dictionary or bytes to send in the body of the Request.
    • headers – (optional) Dictionary of HTTP Headers to send with the Request.
    • cookies – (optional) Dict or CookieJar object to send with the Request.
    • files – (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.
    • auth – (optional) Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth.
    • timeout (float or tuple) – (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, read timeout) tuple.
    • allow_redirects (bool) – (optional) Set to True by default.
    • proxies – (optional) Dictionary mapping protocol to the URL of the proxy.
    • stream – (optional) whether to immediately download the response content. Defaults to False.
    • verify – (optional) if True, the SSL cert will be verified. A CA_BUNDLE path can also be provided.
    • cert – (optional) if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’) pair.
    利用verify参数,直接在POST/GET请求添加verify = False参数,搞定,

    这是HTTPS网站所做的SSL证书认证,默认是True的,设置为False之后对测试没有影响;

    当然如果知道证书的路径,应该也可以用cert设置.pem文件的路径,也可以解决

    #########################################################################################################################################
    在代码第10行更改代码为
    response = requests.get(url, headers=headers, verify=False),程序可以正常运行。
    另外找到其他的解决方法,可是才刚开始学,不知道怎么使用。
    另外的解决方法链接https://www.cnblogs.com/lykbk/p/ASDFQAWQWEQWEQWEQWEQWEQWEQEWEQW.html
    https://blog.csdn.net/wangxiaotian2007/article/details/79284124
  • 相关阅读:
    this指向详解
    领域驱动设计-1-概述
    算法 表达式求值
    进制转换
    IDEA Junit FileNotFoundException: class path resource [spring/spring.xml] cannot be opened because it does not exist
    aes加密示例
    create an oauth app
    搭建docusaurus博客
    Vue项目整体架构记要
    vue+element 获取验证码
  • 原文地址:https://www.cnblogs.com/weiwei2016/p/10162142.html
Copyright © 2011-2022 走看看