首页 > 解决方案 > 部分响应(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)

标签: python-3.xapihttpresponse

解决方案


我不明白你的意思,但简单的正则表达式就可以了

action="(.+)?" method

或者可能

action=\"(.+)?\" method

转义双引号


推荐阅读