在这里插入代码片
#1.TTP协议八种请求方法;http中共定义了八种方法或者叫“动作”来表明对Request-URI指定的资源的不同操作方式,
# 具体介绍如下:
# OPTIONS:返回服务器针对特定资源所支持的HTTP请求方法。也可以利用向Web服务器发送'*'的请求来测试服务器的功能性。
# HEAD:向服务器索要与GET请求相一致的响应,只不过响应体将不会被返回。这一方法可以在不必传输整个响应内容的情况下,就可以获取包含在响应消息头中的元信息。
# GET:向特定的资源发出请求。
# POST:向指定资源提交数据进行处理请求(例如提交表单或者上传文件)。数据被包含在请求体中。POST请求可能会导致新的资源的创建和/或已有资源的修改。
# PUT:向指定资源位置上传其最新内容。
# DELETE:请求服务器删除Request-URI所标识的资源。
# TRACE:回显服务器收到的请求,主要用于测试或诊断。
# CONNECT:HTTP/1.1协议中预留给能够将连接改为管道方式的代理服务器
# get请求
# 第一部分:请求方式 url 协议类型和协议版本
# 第二部分:请求头
# 响应:
# 第一部分:协议类型和协议版本 响应的状态码 ok
# 第二部分:请求方式 url 协议类型和协议版本:
# Connection: keep-alive
# Content-Encoding: gzip
# Content-Language: zh-CN
# Content-Type: text/javascript;charset=UTF-8
# 第三部分:空行
# 第四部分:响应正文
# post请求
#post请求头
#第一部分 请求方式 url 协议/协议类型
#第二部分 请求头:
#connect-type:text/html charset=utf-8
#Cookie:xxxxxxxxxx
#keep-alive:
#host
#第三部分 空行
#第四部分 请求正文
#2.http.client包使用方法:
# 例子
# import http.client
# #新建一个连接
# con=http.client.HTTPConnection('www.baidu.com')
# #发送请求
# con.request('GET','/')
# #获取响应
# a=con.getresponse()
# # print(a.decode())
# print(a.getheaders())
# # print(a.getheader())
# con=http.client.HTTPConnection('10.0.7.244')
# head={
# 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
# }
#
# con.request('GET','/agileone/',headers=head)
# a=con.getresponse().read().decode()
# print(a)
#获取连接对象
# con=http.client.HTTPConnection('10.0.7.244')
#
# #编写请求头信息request head
# head={
# 'Content-Type': 'application/x-www-form-urlencoded'
# }
#
# # 编写请求正文信息request body,服务器会收到这个信息,进行账户密码判断
# date='username=admin&password=admin&savelogin=true'
#
# # 接受cookie
# Cookie=''
#
# #正式请求信息'POST','/agileone/index.php/common/login'中间加上前面的IP'10.0.7.244'成为请求url址
# con.request('POST','/agileone/index.php/common/login',body=date,headers=head)
#
# #接受响应,获取响应对象,
# #这个对象可以提取响应的头和正文的信息;类似前面的连接对象
# response_oj=con.getresponse()#<http.client.HTTPResponse object at 0x0169DF70>是个对象
# # print(type(aa))#<class 'http.client.HTTPResponse'>
#
# response_body=response_oj.read().decode()#通过响应对象获取响应正文,正确是successful 错误是返回user_invalid
# if response_body=='successful':#通过响应对象获取响应头部的各种信息,可以用getheader方法
# Cookie=response_oj.getheader('Set-Cookie')
# print(Cookie)
# print(response_body)
#
#
# # con=http.client.HTTPConnection('10.0.7.244')
# head={
# 'Content-Type': 'application/x-www-form-urlencoded',
# 'Cookie':Cookie
# }
#添加消息
# date="headline=qwe&content=asd&scope=1&expireddate=1999-04-08"
# con.request('POST','/agileone/index.php/notice/add',body=date,headers=head)
# #删除消息
# del_date="noticeid=88"
# con.request('POST','/agileone/index.php/notice/delete',body=del_date,headers=head)
#
# #获取删除后的响应con.getresponse()对像不能和上面一样,因为是两次不同的响应
# response_oj_del=con.getresponse()
# response_body_del=response_oj_del.read().decode()
#
# print(response_oj_del.getheader('Server'))
# print(response_body_del)
# Request库:
# Requests支持HTTP连接保持和连接池,支持使用cookie保持会话,支持文件上传,
# 支持自动确定响应内容的编码,支持国际化的 URL 和 POST 数据自动编码。
#3.requests包使用方法
import requests
#get请求
# head={
# 'asd':'123'
# }
# req=requests.get('http://10.0.7.244/agileone/index.php',headers=head)
# #下面两个都是返回正文
# print(req.content.decode())#正文经过转码
# print(req.text)#这个是正文字节码
# print(req.headers)#这个是请求头部,有所有头部信息的字典
#post请求
# date={
# 'username':'admin',
# 'password':'admin',
# 'savelogin':'true'
# }
# head={
# 'Content-Type':'application/x-www-form-urlencoded'
# }
# req=requests.post('http://10.0.7.244/agileone/index.php/common/login',data=date,headers=head)
# print(req.text)
# # print(req.headers)
# req_head=req.headers
# Cookie=req_head['Set-Cookie']
# print(Cookie)
#
#
# dateadd={
# 'headline':'qwe2333',
# 'content':'asd',
# 'scope':1,
# 'expireddate':'1999-04-08'
# }
# head={
# 'Content-Type':'application/x-www-form-urlencoded',
# 'Cookie':Cookie
# }
# #添加请求
# #reqadd=requests.post('http://10.0.7.244/agileone/index.php/notice/add',data=dateadd,headers=head)
# #删除请求:
# datedel={
# 'noticeid':106
# }
# reqdel=requests.post('http://10.0.7.244/agileone/index.php/notice/delete',data=datedel,headers=head)
#
# # print(reqadd.text)
# print(reqdel.text)
# print(Cookie)
#requests库的cookie子管理 使用session
# session=requests.Session()
# datelogin={
# 'username':'admin',
# 'password':'admin',
# 'savelogin':'true'
# }
# ses=session.post('http://10.0.7.244/agileone/index.php/common/login', data=datelogin)
# print(ses.text)
# if ses.text=='successful':
# dateadd={
# 'headline':'ni cai',
# 'content':'ni cai',
# 'scope':1,
# 'expireddate':'1999-04-08'
# }
# datedel={
# 'noticeid':110
# }
# sesadd=session.post('http://10.0.7.244/agileone/index.php/notice/add', data=dateadd)
# sesdel=session.post('http://10.0.7.244/agileone/index.php/notice/delete', data=datedel)
#
# print(sesadd.text)
# print(sesdel.text)
# else:
# print('登录失败')
2021-01-08
最新推荐文章于 2023-05-11 14:12:36 发布