Python:REST: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
[[Category:Python]] | [[Category:Python]] | ||
REST, REpresentational State Transfer | REST, REpresentational State Transfer | ||
* Transports JSON-formatted data over HTTP | * Transports [[Python:JSON|JSON]]-formatted data over HTTP | ||
* Stateless as the server does not hold client context (history). Pre: simple, Con: sometimes more requests needed. | * Stateless as the server does not hold client context (history). Pre: simple, Con: sometimes more requests needed. | ||
| Line 29: | Line 29: | ||
print ("login status: "+str(response.status_code)) | print ("login status: "+str(response.status_code)) | ||
response = rest_session. | response = rest_session.post(rest_url+command_url,verify=False,stream=True,headers=post_headers) | ||
print ("Command status: "+str(response.status_code)) | print ("Command status: "+str(response.status_code)) | ||
if response.status_code == 200: | if response.status_code == 200: | ||
Latest revision as of 17:38, 25 October 2022
REST, REpresentational State Transfer
- Transports JSON-formatted data over HTTP
- Stateless as the server does not hold client context (history). Pre: simple, Con: sometimes more requests needed.
Example code, fetching data, session state (login information) is kept by the client.
#!/usr/bin/env python
import requests
import json
import sys
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
rest_host = "<servername>"
rest_url = "https://"+rest_host
command_url = "<filepath>"
post_headers = '{"Content-Type":"application/json"}'
def main():
rest_session = requests.Session()
response = rest_session.post(rest_url+"/login",
data={"USERNAME":<user>,"PASSWORD":<password>},
verify=False)
print ("login status: "+str(response.status_code))
response = rest_session.post(rest_url+command_url,verify=False,stream=True,headers=post_headers)
print ("Command status: "+str(response.status_code))
if response.status_code == 200:
print(session.content)
response = rest_session.post(rest_url+"/logout", verify=False)
return()
main()