<jupyter_start><jupyter_text>HTTP WEB的请求<jupyter_code>import sys
import os
import json
import requests<jupyter_output><empty_output><jupyter_text>最简单的请求示例<jupyter_code>url = 'https://www.baidu.com'
req = requests.get(url)
print("status=%d" %(req.status_code))
if req.status_code == 200:
    print("data:\n")
    print(req.text)<jupyter_output>status=200
data:

<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>ç¾åº¦ä¸ä¸ï¼ä½ å°±ç¥é</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=a[...]<jupyter_text>双向证书的请求示例 自己搭建一个要求验证客户端证书的服务端('https://xxxx') 直接访问，将会有SSL报错<jupyter_code>url = 'https://cvm.hansong.io/index.html'

try:
  req = requests.get(url)
  print("status=%d" %(req.status_code))
  if req.status_code == 200:
    print("data:\n")
    print(req.text)
except :
    print("exception:%s||%s" %(sys.exc_info()[0], sys.exc_info()[1]))<jupyter_output>exception:<class 'requests.exceptions.SSLError'>||HTTPSConnectionPool(host='cvm.hansong.io', port=443): Max retries exceeded with url: /index.html (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)'),))<jupyter_text>若加上verify=False, 那么对于双向认证的，依然是返回400错误的！<jupyter_code>url = 'https://cvm.hansong.io/index.html'

try:
  req = requests.get(url,verify = False)
  print("status=%d" %(req.status_code))
  if req.status_code == 200:
    print("data:\n")
    print(req.text)
except :
    print("exception:%s||%s" %(sys.exc_info()[0], sys.exc_info()[1]))<jupyter_output>status=400<jupyter_text>带上客户端证书：<jupyter_code>requests.get('https://kennethreitz.org', cert=('/path/client.cert', '/path/client.key'))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1801042920176494, 'fraction_numerical': 0.02206177296430004, 'mean_word_length': 7.231023102310231, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 47, 'https://': 6, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '499930', 'license': 'Apache-2.0', 'n_tokens_mistral': 1079, 'n_tokens_neox': 1064, 'n_words': 145, 'path': 'jupyter_samples/code/learn_http.ipynb', 'repo_name': 'yangsongx/refrence', 'size': '2493'}