Skip to content
프로젝트

소개

Tornado Web Server용으로 간단하고 유용한 RESTful API

시스템 및 인프라

이 수치를 측정하는 방법

측정된 스타 증가

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

PyRestful

pyRestful is an API to develop restful services with Tornado Web Server. Changes were made from the last version to improve the API.

Last version works with Python 2, 3.5 and 3.7.

Installation

Python version 2.7, 3.5+ are required.

Note: We recommend using Python 3.5+.

Download the api from github (https://github.com/rancavil/tornado-rest/archive/master.zip).

Unzip the file tornado-rest-master.zip

 $ unzip tornado-rest-master.zip

Go to the directory and install.

 $ cd tornado-rest-master
 $ python setup.py install

Or you can install it using.

 $ pip install -U git+https://github.com/rancavil/tornado-rest.git

New release 0.5.1

It was added "syntax sugar" to define services.

 import tornado.ioloop
 import pyrestful.rest

 from pyrestful.rest import get,post

 class EchoService(pyrestful.rest.RestHandler):
      @get('/echo/{name}')
      def sayHello(self, name):
           return {'Hello':name}

 class BookService(pyrestful.rest.RestHandler):
      @post('/book')
      def create_book(self,book):
           # do something...
           return 'Book created...'

 class SomeService(pyrestful.rest.RestHandler):
      @post('/person',{'format' : 'json'}) # content-type (consumes and produces) will be application/json 
      def create_person_json(self,book):
           # do something with book in format json
           return {'status' : 'person created...'} 

If you want to see a complete example, you can go to demo folder and check person_service.py.

Example

The API allows developing a CRUD (Create, Read, Update and Delete) over the resources. In this example the resource is Customer.

First, start the service:

 $ python demos/customer_service.py

Note: you can see customer_service.py is in demos folder.

Creating a new Customer:

 POST: http://myserver.domain.com:8080/customer

 POST /customer HTTP/1.1
 Host: myserver.domain.com
 
 customer_name=Rodrigo&customer_address=Santiago

 POST it is equivalent to INSERT.

Read a Customer:

 GET: http://myserver.domain.com:8080/customer/{id}

 GET /customer/1 HTTP/1.1

 GET it is equivalent to SELECT (READ).

Update a Customer:

 PUT: http://myserver.domain.com:8080/customer/{id}

 PUT /customer/1 HTTP/1.1
 Host: myserver.domain.com
 
 customer_name=Rodrigo&customer_address=Santiago
 
 PUT it is equivalent to UPDATE.

Delete a Customer:

 DELETE: http://myserver.domain.com:8080/customer/{id}

 DELETE /customer/1 HTTP/1.1

 DELETE it is equivalent to DELETE.

PyRestful implements the verbs get, post, put and delete.

Echo Rest Service

This example implements an echo rest service (echo_service.py).

Write the next code and save echo_service.py file.

 import tornado.ioloop
 import pyrestful.rest

 from pyrestful import mediatypes
 from pyrestful.rest import get

 class EchoService(pyrestful.rest.RestHandler):
      @get(_path="/echo/{name}", _produces=mediatypes.APPLICATION_JSON)
      def sayHello(self, name):
           return {"Hello":name}

 if __name__ == '__main__':
      try:
           print("Start the echo service")
           app = pyrestful.rest.RestService([EchoService])
           app.listen(8080)
           tornado.ioloop.IOLoop.instance().start()
      except KeyboardInterrupt:
           print("\nStop the echo service")

You can execute the service.

 $ python echo_service.py

Then in a browser write the url.

 http://localhost:8080/echo/john

You should see the following output in your browser.

 {"Hello": "john"}

관련 프로젝트 살펴보기

불러오는 중…