Python3处理HTTP请求的包:http.client,urllib,urllib3,requests
其中,http 比较 low-level,一般不直接使用
urllib更 high-level一点,属于标准库。urllib3跟urllib类似,拥有一些重要特性而且易于使用,但是属于扩展库,需要安装
requests 基于urllib3 ,也不是标准库,但是使用非常方便
个人感觉,如果非要用标准库,就使用urllib。如果没有限制,就用requests
# import http.client# http_client = http.client.HTTPConnection('localhost',8080,timeout=10)# http_client.request('get','/jenkins/api/json?pretty=true')# response = http_client.getresponse()# print(response.status)# print(response.read())# import urllib.request# response = urllib.request.urlopen('http://localhost:8080/jenkins/api/json?pretty=true')# print(response.status)# print(response.read())# import urllib3# response = urllib3.PoolManager().request('get','http://localhost:8080/jenkins/api/json?pretty=true')# print(response.status)
# import requests# response = requests.get('http://localhost:8080/jenkins/api/json?pretty=true')# print(response.status_code)# print(response.text)# print(response.json())# print(response.reason)
import requestsfrom requests.auth import HTTPBasicAuthresponse = requests.post('http://localhost:8080/jenkins/job/check_python_version/build',auth=('admin','wangmin'))print (response.status_code)print (response.reason)print(response.headers)
jenkins系统管理=》Configure Global Security,取消勾选“防止跨站点请求伪造”