python代码完成POST、GET 请求及响应

# # 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:
    ("登录失败!")
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值