Learn how to use the PySGI library with the complete documentation.
This is the official documentation for using the PySGI library, here you will have the first steps to create a server using our library.
See CHANGELOG.md on GitHub for changes made to each release.
To create a server, you need to import the PySGI class and the Response class (if you need to define content type, cookies, or headers):
from pysgi import PySGI
from pysgi import Response
server = PySGI()
@server.route('/', methods=['GET', 'POST'])
def index(request):
return 'Hello World!'
server.run()
We just created a web server. Let’s see the functionality of some functions and arguments:
PySGI()The PySGI class has the necessary methods to create your application, being only two: route() and run().
There, the routes created by you will also be registered.
server.route()This method is responsible for registering new routes. A route name must always start with ”/”, followed by the route name (eg /about).
The method has only two arguments, the first is route, which requires the name of the route, and the second is methods, which requires a list of methods accepted by this route. The default accepted method is GET, but you can define them in a list.
Method names must be defined in uppercase, for example: GET, POST, PUT, DELETE and others.
@server.route('/user', methods=['DELETE', 'POST'])
index(request)The request argument has information about the client’s request, such as headers, cookies, requested method and route, and URL arguments (or parameters).
This argument is optional, you can put it or not, if you need it, it will be available.
A question… How does PySGI know if the request argument is there? Simple, with one exception, if the
TypeErrorexception is thrown, we will try again to call the function, but without passing the argument.
You can access the data as you access an attribute (eg request.method). Cookies and headers are like dictionaries.
request.body # request body
request.path # request path
request.args # request args
request.method # request method
request.cookies # request cookies
request.headers # request headers
There are two three ways to return a response to the client, let’s see some:
Use this only for simpler requests, the default HTTP status code will be 200.
@server.route('/', methods=['GET', 'POST'])
def index(request):
return 'Hello World' # returning only the body
That way you can return the body and status of the request:
@server.route('/', methods=['GET', 'POST'])
def index(request):
return 'Hello World', 200 # returning the body and status
Response classUsing this class you can have a much more detailed answer by setting body, cookies, headers, content type and status.
@server.route('/', methods=['GET', 'POST'])
def index(request):
return Response('Hello World', content_type='text/plain')
To set cookies and headers (setting their name and value):
@server.route('/', methods=['GET', 'POST'])
def index(request):
response = Response('Hello World', content_type='text/plain')
# cookies
response.set_cookie('Auth', 'utokenAuth123')
# headers
response.set_header('Authorization', 'utokenAuth123')