# # get请求
import http.client
con=http.client.HTTPConnection("www.4399.com")# 新建一个连接
con.request('GET','/')# 此处如果需要写入请求头user-agent
html=con.getresponse().read() # 获取响应
print(html.decode("GBK"))
# get响应
import http.client
con=http.client.HTTPConnection("10.0.7.244")
con.request('GET','/agileone/')
# resp=con.getresponse() #获取响应
# print(resp.getheaders()) #获取所有的头部
# print(resp.getheader('Content-Type')) #获取某个单独键值
# print(resp.getheader('Set-Cookie')) #第一次连接需要获取请求cookie
html=con.getresponse().read().decode()
print(html)
#post请求
import http.client
con=http.client.HTTPConnection('10.0.7.244')
data="username=admin&password=admin&savelogin=true"
head={
"Content-Type":"application/x-www-form-urlencoded"
}
con.request("POST","/agileone/index.php/common/login",body=data,headers=head)
htm=con.getresponse().read().decode()
print(htm)
#
# # 利用cookie保持长时间连接
import http.client
con=http.client.HTTPConnection('10.0.7.244')
data="username=admin&password=admin&savelogin=true"
head={
"Content-Type":"application/x-www-form-urlencoded"
}
con.request("POST","/agileone/index.php/common/login",body=data,headers=head)
rsp=con.getresponse()
html=rsp.read().decode()
print(html)
Coo=""
if html=="successful":
Coo=rsp.getheader('Set-Cookie')
print(Coo)
print("登陆成功")
#
# 添加
con = http.client.HTTPConnection('10.0.7.244')
data ="headline=test111&content=content111&scope=1&expireddate=2021-04-08"
head = {
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": Coo
}
con.request("POST", "/agileone/index.php/notice/add", body=data, headers=head)
r = con.getresponse().read().decode()
print(r)
#删除
con = http.client.HTTPConnection('10.0.7.244')
data ="noticeid=73"
head = {
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": Coo
}
con.request("POST", "/agileone/index.php/notice/delete", body=data, headers=head)
r = con.getresponse().read().decode()
print(r)
#requests库的使用
import requests #引包
#requests完成get请求
head={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
}
resp=requests.get("http://10.0.7.244/agileone/index.php",headers=head)
print(resp.content.decode())
#requests完成poost请求
head={
"Content-Type": "application/x-www-form-urlencoded",
}
data={
"username":"admin",
"password":"admin",
"savelogin":"true"
}
resp=requests.post("http://10.0.7.244/agileone/index.php/common/login",headers=head,data=data)
login=resp.text # print(resp.content.decode())
coo=""
if login=="successful":
coo = resp.headers.get('Set-Cookie')
print(coo)
print("登陆成功")
pack ={"headline":"who",
"content":"wangt",
"scope":"1",
"expireddate":"2021-04-08"
}
head = {
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": coo
}
respl=requests.post("http://10.0.7.244/agileone/index.php/notice/add",headers=head,data=pack)
loginl=respl.text
print(loginl)
# requests 的cookie自管理 进行提交操作
import requests #引包
se=requests.Session()
date={
"username":"admin",
"password":"admin",
"savelogin":"true"
}
respon=se.post("http://10.0.7.244/agileone/index.php/common/login",data=date)
print(respon.text)
if respon.text=="successful":
pack={
"headline": "who",
"content": "wangt",
"scope": "1",
"expireddate": "2021-04-08"
}
log= se.post("http://10.0.7.244/agileone/index.php/notice/add", data=pack)
print(log.text)
else:
("登录失败!")
python代码完成POST、GET 请求及响应
最新推荐文章于 2024-05-10 16:20:06 发布