Skip to content

Instantly share code, notes, and snippets.

@EserGozcu
Last active November 13, 2018 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EserGozcu/3daa5da49eefe11bb052335f637df249 to your computer and use it in GitHub Desktop.
Save EserGozcu/3daa5da49eefe11bb052335f637df249 to your computer and use it in GitHub Desktop.
import requests #request library is to send HTTP requests
url_system_status = "http://127.0.0.1:8384/rest/system/config"
#according to the documentation of Syncthing,/rest/system/config exposes configuration details
headers= {"X-API-Key" : "XXXXXXXXXXX"}
"""
headers is for authentication. Syncthing REST API exposes the end points via API Key
API key can be found at Syncthing GUI --> Actions --> Settings
This is to ensure that the information seen at the end point is not visible to unauthorised users.
"""
response = requests.get(url_system_status,headers=headers).json()
"""
response variable is where the real HTTP GET request happening.
Basically it says that, use the requests libabry which has already been imported
and execute the get function for url_system_status (which is http://127.0.0.1:8384/rest/system/config)
and include headers which is the X-API-KEY itself
At this point we already have the result saved into response variable.
.json() #This is to convert incoming info into a Python list of dictionaries
"""
for key,values in response.items():
print (key,values)
"""
Since a dictionary is a combination of keys and values, then we can iterate over each key with the corresponding value
and print them one by one. This part of the code is not a must but it is nice to read the response.
"""
with open('config.json', 'w') as config_file:
json.dump(response, config_file, indent=4)
"""
by using json.dump we can write the result to a file which can be used later for a reference or for a config change.
This code will create config.json in the same directory where the script is. response will be used to create the content of this file.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment