python-3.x - 部分响应(API)
问题描述
我已经完成了以下脚本。
def token() :
url = "https://" +ip_add + ":8443/cas/v1/tickets"
payload = 'username=' +usr + '&password='+ pwd
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Type': 'application/x-www-form-urlencoded'
}
response = requests.post(url, headers=headers,data = payload, verify=False)
response.raise_for_status()
print(response.text)
if response.status_code == 201:
print(response.status_code , "OK", " >>> Normal")
else:
print("Status : ", response.status_code)
并给我输出:
<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head><title>201 Created</title></head><body><h1>TGT Created</h1><form action="https://xx.xx.xx.xx:8443/cas/v1/tickets/TGT-86-wFHhWf7Yt5e1WkYggRcDwvZx9H1x05LlfdFFG6ZfVEwL196oC7-cas01.example.org" method="POST">Service:<input type="text" name="service" value=""><br><input type="submit" value="Submit"></form></body></html>
201 OK >>> Normal
所以我只需要打印或保存此输出的一部分,即:
https://xx.xx.xx.xx:8443/cas/v1/tickets/TGT-86-wFHhWf7Yt5e1WkYggRcDwvZx9H1x05LlfdFFG6ZfVEwL196oC7-cas01.example.org
解决方案:对于任何正在寻找解决方案的人,我已经尝试了以下方法,它对我有用。
tgt = response.headers['location']
print(tgt)
解决方案
我不明白你的意思,但简单的正则表达式就可以了
action="(.+)?" method
或者可能
action=\"(.+)?\" method
转义双引号
推荐阅读
- javascript - windows.scroll 在 chrome 中工作,但在 safari 中不工作
- reactjs - 上传失败时使用 dropzone-react-component 设置重试
- python - Docker:无法使用请求库请求另一个容器
- algorithm - 如何创建将两个值映射为一个的算法
- jquery - 有没有办法让 jQuery 函数在第一次加载时立即工作?
- php - Google reCaptcha 3 - 第一次失败后登录错误
- scheme - 如何得到这个函数的逆?
- r - 在 R ggplot2 中:如何将 scale_fill_manual 中的颜色分配给一个因子?
- postgresql - Slick 有效地组合查询条件
- elasticsearch - 打开 Distro 警报插件 elasticsearch 异常