-1

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")
Toulon7559
  • 31
  • 10
  • It is not really clear what you are trying to achieve here. You should state your problem/question properly to make sure people can help you. – Alex Garcia Nov 25 '16 at 13:16
  • Description of objective: – Toulon7559 Nov 25 '16 at 16:27
  • Description of Objective: 1) Have 2* Domoticz @ Raspberry running sensors & switches Domoticz has own display at LAN's computers by means of it's integrated webserver 2) Do not want access from WWW to that 'internal' webserver = no 'pulling' 3) Have website running at WWW through www.vannwnhzn.nl 4) Would like to have data from Domotiz included in that WWW-website by means of Javascript(s) That requires that I upload (= 'pushing') either JSON-file(s) or XML-file(s) for 'dissect'by the Javascript(s) – Toulon7559 Nov 25 '16 at 16:50
  • Nest part of Objective 5) Requirement translates to: a) Make JSON-call to selected Domoticz' RID b) Convert output of that call into JSON-file and/or XML-file suitable for upload to the website-server c) Make Upload-call for these files With the code shown (with a test-content), a) and c) seem achieved, but b) 'misses something'. Have tried myself with something like json_output = open("json89_upload.json",'w') json_output.write(obj_test) json_output.close() But get error report that 'obj_test' is not accepted in the write-line ........ – Toulon7559 Nov 25 '16 at 16:55
  • Searching further in StackOverflow probably found the solution myself: needed to implement jsondump to get the code in the right format. – Toulon7559 Nov 25 '16 at 20:52

1 Answers1

0

The code below is making all required calls and conversions, as well as the upload to a selected ftp-server on LAN or on Internet. Next step is extraction of info from the JSON-file or XML-file, but that is another aspect than this question.

#!/usr/bin/python
# -*- coding = utf-8 to enable reading by simple editors -*-
# (c)2016 script compiled by Toulon7559
# --------------------------------------------------
# Line005 = PREPARATION & SETTING
# --------------------------------------------------
# Imports for script-operation
import json
import urllib
import dicttoxml
page = urllib.urlopen('http://<source-IP-address>:8080
/json.htm?type=devices&rid=87')

content_test = page.read()
obj_test = json.loads(content_test)
print(obj_test)
with open('json87_upload.json', 'w') as outfile:
    json.dump(obj_test, outfile)
xml_test = dicttoxml.dicttoxml(obj_test)
print(xml_test)
xml_output = open("xml87_upload.xml",'w')
xml_output.write(xml_test)
xml_output.close()
# --------------------------------------------------
# Line024 = 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)

# --------------------------------------------------
# Line038 = Actual FTP-Login & -Upload
# --------------------------------------------------
ftp = ftplib.FTP("<ftp-server>")
ftp.login("<ftp_username>", "<ftp_password>")

upload(ftp, "json87_upload.json")
upload(ftp, "xml87_upload.xml")
Toulon7559
  • 31
  • 10