python请求异常-不断抛出错误

时间:2018-08-14 15:00:47

标签: python python-requests

当接收到HTTP 200时脚本可以正常工作。但是,一旦出现异常,我就会收到错误消息。我知道该网站没有安装有效的SSL证书,并且我尝试过request.exceptions.RequestException来捕获任何内容,但仍然会引发错误。我还会不时出现诸如代理错误之类的异常,而requests.exceptions.RequestException不会捕获该异常。我也尝试过,在try:request.raise_for_status()下,仍然抛出错误。代码如下:

def verify_ssl(proxy_info, target): 
   print('Attempting to verify SSL Cert on %s:443' % target)
   if proxy_info == None:
      response = requests.get('https://%s' % target)
   if proxy_info != None:
      response = requests.get('https://%s' % target, proxies=proxy_info)
   try:
      response
   except requests.exceptions.SSLError as g:
      print('SSL Verification Error %s' % g)
   return ('Successfully Verified SSL Cert: HTTP 200 received.\n')

Debug抛出这个:

    Traceback (most recent call last):
  File "c:\Users\appleta\Desktop\ping.py", line 69, in <module>
    ssl_result = verify_ssl(proxy_info, target)
  File "c:\Users\appleta\Desktop\ping.py", line 37, in verify_ssl
    response = requests.get('https://%s' % target)
  File "C:\Python34\lib\site-packages\requests\api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Python34\lib\site-packages\requests\api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Python34\lib\site-packages\requests\sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python34\lib\site-packages\requests\sessions.py", line 622, in send
    r = adapter.send(request, **kwargs)
  File "C:\Python34\lib\site-packages\requests\adapters.py", line 511, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='blabla.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:600)'),))

1 个答案:

答案 0 :(得分:2)

在python中,表达式:

./ffprobe -v quiet -print_format json -show_format -show_streams testoutput.mp4

不能从不产生异常。您必须将两个 ./ffprobe -v quiet -print_format json -show_format -show_streams testoutput.mp4 { "streams": [ { "index": 0, "codec_name": "h264", "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10", "profile": "Main", "codec_type": "video", "codec_time_base": "1001/48000", "codec_tag_string": "avc1", "codec_tag": "0x31637661", "width": 1280, "height": 720, "coded_width": 1280, "coded_height": 720, "has_b_frames": 0, "sample_aspect_ratio": "1:1", "display_aspect_ratio": "16:9", "pix_fmt": "yuv420p", "level": 40, "chroma_location": "left", "refs": 1, "is_avc": "false", "nal_length_size": "0", "r_frame_rate": "24000/1001", "avg_frame_rate": "24000/1001", "time_base": "1/24000", "start_pts": 984, "start_time": "0.041000", "duration_ts": 1201200, "duration": "50.050000", "bit_rate": "845035", "bits_per_raw_sample": "8", "nb_frames": "1199", "disposition": { "default": 1, "dub": 0, "original": 0, "comment": 0, "lyrics": 0, "karaoke": 0, "forced": 0, "hearing_impaired": 0, "visual_impaired": 0, "clean_effects": 0, "attached_pic": 0, "timed_thumbnails": 0 }, "tags": { "language": "und", "handler_name": "VideoHandler" } }, { "index": 1, "codec_name": "aac", "codec_long_name": "AAC (Advanced Audio Coding)", "profile": "LC", "codec_type": "audio", "codec_time_base": "1/48000", "codec_tag_string": "mp4a", "codec_tag": "0x6134706d", "sample_fmt": "fltp", "sample_rate": "48000", "channels": 2, "channel_layout": "stereo", "bits_per_sample": 0, "r_frame_rate": "0/0", "avg_frame_rate": "0/0", "time_base": "1/48000", "start_pts": 0, "start_time": "0.000000", "duration_ts": 2401488, "duration": "50.031000", "bit_rate": "129153", "max_bit_rate": "129153", "nb_frames": "2346", "disposition": { "default": 1, "dub": 0, "original": 0, "comment": 0, "lyrics": 0, "karaoke": 0, "forced": 0, "hearing_impaired": 0, "visual_impaired": 0, "clean_effects": 0, "attached_pic": 0, "timed_thumbnails": 0 }, "tags": { "language": "ger", "handler_name": "SoundHandler" } } ], "format": { "filename": "Leonmusssterben20171080p.mp4", "nb_streams": 2, "nb_programs": 0, "format_name": "mov,mp4,m4a,3gp,3g2,mj2", "format_long_name": "QuickTime / MOV", "start_time": "0.000000", "duration": "50.031000", "size": "6116472", "bit_rate": "978029", "probe_score": 100, "tags": { "major_brand": "isom", "minor_version": "512", "compatible_brands": "isomiso2avc1mp41", "encoder": "Lavf58.17.100" } } } 调用都放在response 块中:

get

也:请注意,您总是返回字符串try,因为即使出现错误,您也只打印错误消息,然后恢复执行。您可能想在def verify_ssl(proxy_info, target): print('Attempting to verify SSL Cert on %s:443' % target) try: if proxy_info is None: response = requests.get('https://%s' % target) else: response = requests.get('https://%s' % target, proxies=proxy_info) except requests.exceptions.SSLError as g: print('SSL Verification Error %s' % g) return 'Got SSL error' return 'Successfully Verified SSL Cert: HTTP 200 received.\n' 块中返回不同的内容。