开发者

Creating REST Web Services with Python

开发者 https://www.devze.com 2023-01-13 00:56 出处:网络
开发者_JAVA技巧Is it possible to create REST Web Services, that returns JSON or XML, using Python ?

开发者_JAVA技巧Is it possible to create REST Web Services, that returns JSON or XML, using Python ?

Could you give me some recomandations ?

Thank you.


Short answer: Yes. You certainly can do it with Python, using or without using one of the several tools available for the task:

  • django-piston
  • Werkzeug
  • Pylons
  • appengine-rest-server

A bigger answer is difficult (and pointless) without knowing more about your requirements. I suggest that you explore the various options and decide for yourself.


Take a look at RESTx. It's fully open source, written in Python and runs in the JVM, so you can write custom components in Python or Java. It's specialized on the creation of RESTful web services. Components are reusable and you create new RESTful web services by merely sending a new component configuration to the server (via a simple RESTful API or by filling out a small form in a web server). I think RESTx is probably the quickest and simplest way to create RESTful web services.

Disclaimer: I'm the lead developer of RESTx. So, if you have any questions, just let me know. I'd be happy to help.


I'm surprised nobody mentioned the Flask's plugin. http://flask-restful.readthedocs.org/en/latest/

I didn't use personaly (at least not yet), but i have an extensive use of Flask which is pretty handfull. So I don't see how things can get wrong using this plugin.


For creating web services in Python, the easiest way is using Flask. Flask is a popular web framework written in Python, used for development of web application.

Step 1: Open any Text Editor and write the following code I’ll be using Atom you can use notepad or notepad++ or any other editor, inside the file app.py.

from flask import Flask
app = Flask(__name__)
@app.route('/')
  def index():
  return "Hello World"
if __name__ == '__main__':
  app.run()

This is a simple Hello World program.

Step2:

from flask import Flask, jsonify
app = Flask(__name__)
Student = [
{
'id': 1,
'firstName': 'Aditya',
'lastName': 'Malviya',
'age': '24'
},
{
'id': 2,
'firstName': 'Aman',
'lastName': 'Mehta',
'age': '25'
},
{
'id': 3,
'firstName': 'Nuclear',
'lastName': 'Geeks',
'age': '26'
}
]


@app.route('/Student/', methods=['GET'])
   def get_Student():
   return jsonify({'tasks': Student})
if __name__ == '__main__':
  app.run()

So we’ve create an array of dictionary in our memory, Here we have get_Student() function which will fetch all the Students from our memory. This is the example of GET Request. To run the above copy and paste this to your browser “http://127.0.0.1:5000/Student/” and hit Enter you’ll get the list of students or enter the following command in the terminal and hit Enter.

curl -i http://localhost:5000/Student/

You can even checkout the link https://nucleargeeks.wordpress.com/2018/08/31/rest-ing-with-flask/


Sure, you can use any web framework you like, just set the content-type header to the mime type you need. For generating json I recommend the simplejson module (ranamed to json and included in the standard library since 2.6), for handling XML the lxml library is very nice.

0

精彩评论

暂无评论...
验证码 换一张
取 消