Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Generate (and download) JSON file from hosted site using server side python

morgan samage

New Coder
Hope the following makes sense. It is something that I have never had to do this before, so I am basically looking for ideas/best practices.

I have a hosted website (currently Wordpress, but I could change this to flask?) that contains a directory, that contains a json file, e.g.
https://my_domain.com/my_folder/my_file.json

From a python app running locally, I want to download the json file, but before the file is downloaded, I want to run a server side python script that will extract the latest information from a mysql database to update the json file. I then want to return the json file to the client side python app.

As mentioned, I have never needed to do this before so I am not sure of the correct/best way to do it, or if wordpress will even interface with python in this way. Solutions that I have thought of so far, include: -

1) Interface directly with the hosted database from the client side python app.
I could connect to the hosted database from the client side python app to access the information that I want, but I don't really like this idea.

2) Add a trigger to the hosted database
I thought about adding a trigger to the hosted database so that each time new data is written, the json file is updated. However, the only way I can see this working would be to call a python script from the trigger that would update the json file. I did read somewhere that the database would need an extension for this to work, and I don't think that will be an option from the hosting company?

3) Periodically run a server side python script
I could periodically run a python script on the hosted site using a cron job to update the json file, but this will be akin to polling and is not the right solution.

So, I hope someone has some better ideas on this than me, and that this is the correct forum for this type of question?
 
changing the JSON file requires have access to the server/file system where it is stored. It would be a matter of
Code:
with open(pathtofile, 'w') as f:
f.write(NEWDATA)

reading the file (well if you have access to it server side)
same code, but open file as 'r' instead, read up on file like objects
you can load JSON to object with module like
Code:
import json 
obj = json.loads(textdata)

txtdata = json.dumps(object)


if its a remote file, use requests/urllib3 to HTTP GET the file and read it
Code:
import requests / urllib
q = s.get(remote_url_to_file)
q.text   is contents of file
 
Hope the following makes sense. It is something that I have never had to do this before, so I am basically looking for ideas/best practices.

I have a hosted website (currently Wordpress, but I could change this to flask?) that contains a directory, that contains a json file, e.g.
https://my_domain.com/my_folder/my_file.json

From a python app running locally, I want to download the json file, but before the file is downloaded, I want to run a server side python script that will extract the latest information from a mysql database to update the json file. I then want to return the json file to the client side python app.

As mentioned, I have never needed to do this before so I am not sure of the correct/best way to do it, or if wordpress will even interface with python in this way. Solutions that I have thought of so far, include: -

1) Interface directly with the hosted database from the client side python app.
I could connect to the hosted database from the client side python app to access the information that I want, but I don't really like this idea.

2) Add a trigger to the hosted database
I thought about adding a trigger to the hosted database so that each time new data is written, the json file is updated. However, the only way I can see this working would be to call a python script from the trigger that would update the json file. I did read somewhere that the database would need an extension for this to work, and I don't think that will be an option from the hosting company?

3) Periodically run a server side python script
I could periodically run a python script on the hosted site using a cron job to update the json file, but this will be akin to polling and is not the right solution.

So, I hope someone has some better ideas on this than me, and that this is the correct forum for this type of question?
Hi there,
You might want to be careful with what you are intending to do. This may be a big security risk.
 
Back
Top Bottom