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

2024/11/16 0:02:31

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

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")
https://en.xdnf.cn/q/118992.html

Related Q&A

python pygame mask collision [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.The com…

How to find max average of values by converting list of tuples to dictionary?

I want to take average for all players with same name. I wrote following code. its showing index error whats the issue?Input: l = [(Kohli, 73), (Ashwin, 33), (Kohli, 7), (Pujara, 122),(Ashwin, 90)]Out…

Cannot pass returned values from function to another function in python

My goal is to have a small program which checks if a customer is approved for a bank loan. It requires the customer to earn > 30k per year and to have atleast 2 years of experience on his/her curren…

What is the difference here that prevents this from working?

Im reading a list of customer names and using each to find an element. Before reading the list, I make can confirm this works when I hard-code the name,datarow = driver.find_element_by_xpath("//sp…

How can I extract numbers based on context of the sentence in python?

I tried using regular expressions but it doesnt do it with any context Examples:: "250 kg Oranges for Sale" "I want to sell 100kg of Onions at 100 per kg"

Chart with secondary y-axis and x-axis as dates

Im trying to create a chart in openpyxl with a secondary y-axis and an DateAxis for the x-values.For this MWE, Ive adapted the secondary axis example with the DateAxis example.from datetime import date…

Env var is defined on docker but returns None

I am working on a docker image I created using firesh/nginx-lua (The Linux distribution is Alpine): FROM firesh/nginx-luaCOPY ./nginx.conf /etc/nginx COPY ./handler.lua /etc/nginx/ COPY ./env_var_echo.…

No Browser is Open issue is coming when running the Robot framework script

I have created Test.py file which has some function in it and using those function names as Keywords in sample.robot file. Ex: - Test.pydef Launch_URL(url):driver.get(url)def article(publication): do…

How to run python file in Tkinter

Im a beginner in Python, hence the question. i would like to run a python file (smileA.py) in Tkinter. How would i start? I do not wish for it to run when clicking a button, but the file to run automa…

Discord.py Cogs “Command [ ] is not found”

I am recoding my discord.py bot using cogs as it wasnt very nice before. I tried this code: import discord import os import keep_alive from discord.ext import commands from discord.ext.commands import …