从JSON-Call制作上传文件所需的Python脚本行 [英] Python-scriptlines required to make upload-files from JSON-Call

查看:122
本文介绍了从JSON-Call制作上传文件所需的Python脚本行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

缺乏经验和用于Python编程的例程. 论坛中的借用"示例制作了一个脚本,该脚本成功制作了JSON-cal(对Domoticz而言)并生成了&打印派生的JSON文件和XML文件:请参见下文,并带有通用"地址.登录信息等. 但是,为了将这些JSON文件和XML文件进一步上传到另一台计算机,显然缺少文件转换,因为第38和39行给出了错误报告. 因此,请求帮助/提示/示例来填写以下脚本的第14和17行以及第27行可能也需要进行相应的调整. [第10行被打破"以适合代码块]

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

推荐答案

下面的代码进行所有必需的调用和转换,以及将其上传到LAN或Internet上的选定ftp服务器. 下一步是从JSON文件或XML文件中提取信息,但这是除此问题之外的另一方面.

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

这篇关于从JSON-Call制作上传文件所需的Python脚本行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆