Lacking experience & routine for Python-programming.
'Borrowing' examples from forums have made a script which successfully makes a JSON-cal (to Domoticz) and generates & prints the derived JSON-file and XML-file: see below, with 'generalized' adresses. login-info etc..
But for further upload of those JSON-file and XML-file to another computer apparently a file-conversion is missing, because lines 38 and 39 give an error report.
Therefore assistance/hints/examples requested to fill in lines 14 and 17 of the below script, and probably also line 27 needs a related adaptation.
[line 10 is 'broken' to fit in the code-block]
#!/usr/bin/python
# -*- coding = utf-8 to enable reading by simple editors -*-
# ------------------------------------------------------
# Line004 = PREPARATION & SETTING & JSON-call & Process
# ------------------------------------------------------
# Imports for script-operation
import json
import urllib
import dicttoxml
page = urllib.urlopen('http://<source-ip-address>:8080
/json.htm?type=devices&rid=89')
content_test = page.read()
obj_test = json.loads(content_test)
print(obj_test)
# HERE A LINE IS MISSING to convert obj_test for upload in line 38
xml_test = dicttoxml.dicttoxml(obj_test)
print(xml_test)
# HERE A LINE IS MISSING to convert xml_test for upload in line 39
# -----------------------------------------------------
# Line019 = Function for FTP_UPLOAD to Server
# -----------------------------------------------------
# Imports for script-operation
import ftplib
import os
# Definition of Upload_function
def upload(ftp, file):
ext = os.path.splitext(file)[1]
if ext in (".txt", ".htm", ".html"):
ftp.storlines("STOR " + file, open(file))
else:
ftp.storbinary("STOR " + file, open(file, "rb"), 1024)
# -----------------------------------------------------
# Line033 = Actual FTP-Login & -Upload
# -----------------------------------------------------
ftp = ftplib.FTP("<destination-ftp-server>")
ftp.login("<username>", "<password>")
upload(ftp, "obj_test")
upload(ftp, "xml_test")
# upload(ftp, "file.txt")
# upload(ftp, "sightings.jpg")
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…