响应部分(API

问题描述 投票:0回答:1

我已经做了下面的脚本。

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.x api httpresponse
1个回答
0
投票

我不明白你的意思,但简单的regex就可以了。

action="(.+)?" method

或者

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

去掉双引号

© www.soinside.com 2019 - 2024. All rights reserved.