Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

Python-scriptlines required to make upload-files from JSON-Call

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...