Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从JSON调用上传文件所需的Python脚本行_Python_Json_Xml - Fatal编程技术网

从JSON调用上传文件所需的Python脚本行

从JSON调用上传文件所需的Python脚本行,python,json,xml,Python,Json,Xml,缺乏Python编程的经验和常规。 来自论坛的“借用”示例制作了一个脚本,该脚本成功地生成了JSON cal(到Domoticz),并生成和打印了派生的JSON文件和XML文件:见下文,带有“通用”地址。登录信息等。。 但对于进一步将这些JSON文件和XML文件上传到另一台计算机,显然缺少文件转换,因为第38行和第39行给出了错误报告。 因此,填写以下脚本第14行和第17行所需的帮助/提示/示例,以及第27行可能也需要相关的修改。 [第10行“断开”以适合代码块] #!/usr/bin/pyth

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

#!/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")
#/usr/bin/python
#-*-编码=utf-8,可通过简单编辑器进行读取-*-
# ------------------------------------------------------
#Line004=准备和设置以及JSON调用和过程
# ------------------------------------------------------
#为脚本操作导入
导入json
导入URL库
导入dictoxml
page=urllib.urlopen('http://:8080
/json.htm?type=devices&rid=89')
content\u test=page.read()
obj_test=json.loads(content_test)
打印(obj_测试)
#此处缺少一行,用于将obj_测试转换为第38行中的上载
xml_测试=dicttoxml.dicttoxml(obj_测试)
打印(xml_测试)
#在这里,第39行中缺少一行用于将xml_测试转换为上载的内容
# -----------------------------------------------------
#Line019=FTP_上传到服务器的功能
# -----------------------------------------------------
#为脚本操作导入
进口ftplib
导入操作系统
#上传功能的定义
def上传(ftp,文件):
ext=os.path.splitext(文件)[1]
如果ext在(“.txt”、“.htm”、“.html”)中:
ftp.storlines(“STOR”+文件,打开(文件))
其他:
ftp.storbinary(“STOR”+文件,打开(文件,“rb”),1024)
# -----------------------------------------------------
#Line033=实际FTP登录和上传
# -----------------------------------------------------
ftp=ftplib.ftp(“”)
ftp.login(“,”)
上传(ftp,“obj_测试”)
上传(ftp,“xml_测试”)
#上传(ftp,“file.txt”)
#上传(ftp,“sightings.jpg”)

下面的代码正在进行所有必需的调用和转换,以及上传到LAN或Internet上选定的ftp服务器。 下一步是从JSON文件或XML文件中提取信息,但这是与此问题不同的另一个方面

#!/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")
#/usr/bin/python
#-*-编码=utf-8,可通过简单编辑器进行读取-*-
#(c)Toulon7559编制的2016年脚本
# --------------------------------------------------
#Line005=准备和设置
# --------------------------------------------------
#为脚本操作导入
导入json
导入URL库
导入dictoxml
page=urllib.urlopen('http://:8080
/json.htm?type=devices&rid=87')
content\u test=page.read()
obj_test=json.loads(content_test)
打印(obj_测试)
以open('json87_upload.json','w')作为输出文件:
json.dump(obj_测试,outfile)
xml_测试=dicttoxml.dicttoxml(obj_测试)
打印(xml_测试)
xml_output=open(“xml87_upload.xml”,“w”)
xml_output.write(xml_测试)
xml_output.close()
# --------------------------------------------------
#Line024=FTP\u上传到服务器
# --------------------------------------------------
#为脚本操作导入
进口ftplib
导入操作系统
#上传功能的定义
def上传(ftp,文件):
ext=os.path.splitext(文件)[1]
如果ext在(“.txt”、“.htm”、“.html”)中:
ftp.storlines(“STOR”+文件,打开(文件))
其他:
ftp.storbinary(“STOR”+文件,打开(文件,“rb”),1024)
# --------------------------------------------------
#Line038=实际FTP登录和上传
# --------------------------------------------------
ftp=ftplib.ftp(“”)
ftp.login(“,”)
上传(ftp,“json87_upload.json”)
上传(ftp,“xml87_upload.xml”)

现在还不清楚您想要在这里实现什么。你应该正确地陈述你的问题,以确保人们能帮助你。目标描述:目标描述:1)让2*Domoticz@Raspberry运行传感器和开关Domoticz通过其集成的Web服务器在局域网的计算机上有自己的显示器2)不想从WWW访问该“内部”Web服务器=不“拉”3) 通过WWW.vannwnhzn.nl 4)在WWW上运行的网站希望通过Javascript将来自Domotiz的数据包含在该WWW网站中,该Javascript要求我上传(‘推送’)JSON文件或XML文件,以便Javascript进行‘剖析’目标5)要求的嵌套部分转化为:a)对选定的Domoticz'RID进行JSON调用b)将该调用的输出转换为适合上传到网站服务器的JSON文件和/或XML文件c)对这些文件进行上传调用,显示代码(带有测试内容),a)和c)似乎实现了,但b)“遗漏了一些东西”。我曾经尝试过类似于json_output=open(“json89_upload.json”,“w”)json_output.write(obj_test)json_output.close()的东西,但得到的错误报告是,在写入行中不接受“obj_test”。。在StackOverflow中进一步搜索可能自己找到了解决方案:需要实现jsondump以获得正确格式的代码。