Python Forum
How to access a web service from a python script?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to access a web service from a python script?
#1
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?
Reply
#2
Use requests.
Reply
#3
(Dec-03-2020, 07:02 PM)stranac Wrote: Use requests.

Thanks. I will check this out.
Reply
#4
disregard this message.
Reply
#5
(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?
Reply
#6
A response object (your p) has a .json() method.
It's used in the first code snippet you see on the website.
Reply
#7
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help for script access via webdriver to an open web page in Firefox Clixmaster 1 1,271 Apr-20-2023, 05:27 PM
Last Post: farshid
  Creating a RESTful web service and web service client dangermaus33 0 1,284 Dec-01-2020, 09:56 PM
Last Post: dangermaus33
  Service Now CSV Export Redirection Python mavecnirp 1 2,800 Jul-24-2018, 04:09 PM
Last Post: nilamo
  How to access py script variables through web app ? BennyBubble 1 2,411 Mar-16-2018, 09:43 AM
Last Post: thomasp
  python 3.x and selenium. Service unexpectedly exited volkoshkursk 0 3,659 Aug-12-2017, 12:17 PM
Last Post: volkoshkursk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020