Python Forum

Full Version: How to access a web service from a python script?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have been trying to create a web service using python and flask. I have a simple one created but I am unsure how to access it from code. I want a client python script that gets and puts data into the database through the web service. In the web service, I connect to the database and have my SELECT and INSERT functions as well.

In the examples I have found I see curl commands used to access the web service. How do I do this from a Python script?
(Dec-03-2020, 07:02 PM)stranac Wrote: [ -> ]Use requests.

Thanks. I will check this out.
disregard this message.
(Dec-03-2020, 07:02 PM)stranac Wrote: [ -> ]Use requests.

I have this working somewhat. I used a list to retrieve the data from the database in the web service. I use this line to return the list to the client:

return jsonify(products)
When I use the line below to get the data I can print the text of it but I would like it in a list.

p = requests.get("http://127.0.0.1:5000/")
print(p.text)
If I convert the p.text to a list
(products = list(p.text)
then every single character is its own item in the list.

How can I get a list or dictionary from the web service using requests?
A response object (your p) has a .json() method.
It's used in the first code snippet you see on the website.
(Dec-04-2020, 02:48 AM)stranac Wrote: [ -> ]A response object (your p) has a .json() method.
It's used in the first code snippet you see on the website.

Thanks again. I think I have it all figured out now.