import
requests
import
json
api_url
=
"http://localhost:1337/"
username
=
"aaa"
password
=
"123"
image_file
=
"test.conf"
label
=
"test"
def
register(username, password):
url
=
api_url
+
"/api/register"
payload
=
json.dumps({
"username"
: username,
"password"
: password
})
headers
=
{
'Content-Type'
:
'application/json'
}
response
=
requests.request(
"POST"
, url, headers
=
headers, data
=
payload)
return
response
def
login(username, password):
url
=
api_url
+
"/api/login"
payload
=
json.dumps({
"username"
: username,
"password"
: password
})
headers
=
{
'Content-Type'
:
'application/json'
}
connection
=
requests.Session()
response
=
connection.request(
"POST"
, url, headers
=
headers, data
=
payload)
return
response,connection
def
upload(connection, filename, label):
url
=
api_url
+
"/api/upload"
with
open
(filename,
"rb"
) as f:
data
=
f.read()
files
=
{
'file'
: data}
values
=
{
'label'
: label}
response
=
connection.request(
"POST"
, url, files
=
files, data
=
values)
return
response
def
gallery(connetion):
url
=
api_url
+
"/api/gallery"
return
connetion.get(url)
def
log_config(connection, filename):
url
=
api_url
+
"/api/log_config"
logConf
=
{
"filename"
:
"../images/"
+
filename}
connection.post(url, json
=
logConf)
def
readCmd(connection):
url
=
api_url
+
"static/docs/cmd.txt"
return
connection.get(url)
def
main():
response
=
register(username, password)
print
(response.text)
response, connection
=
login(username, password)
print
(response.text)
response
=
upload(connection, image_file, label)
print
(response.text)
response
=
gallery(connection)
print
(response.text)
log_config(connection, json.loads(response.text)[
"message"
][
-
1
])
response
=
readCmd(connection)
print
(response.text)
![图片描述](upload
/
tmp
/
952339_9TBMQQZQHMQ8N3H
.png)
if
__name__
=
=
"__main__"
:
main()