DEV Community

Marcin Wosinek
Marcin Wosinek

Posted on • Originally published at how-to.dev

How to start a server with python framework Pyramid

This article shows how to start a simple server with Pyramid.

Requirements

First, you need the following programs available on your system:

  • python3
  • pip
  • venv

It all should be provided with a python package in your operating system.

Starting virtual environment

To avoid polluting the global scope with package versions, we can start & use the virtual environment:

$ python3 -m venv .venv
Enter fullscreen mode Exit fullscreen mode

When successful, this command creates .venv folder & produces no output.

To switch to the virtual env:

$ source .venv/bin/activate
(.venv) $
Enter fullscreen mode Exit fullscreen mode

The current console will use .venv as the location for packages managed by pip.

Installing the dependency

(.venv) $ pip install pyramid                                                                                                                                                              
Collecting pyramid                                                                                                                                                                     
  Downloading pyramid-2.0-py3-none-any.whl (246 kB)                                                                                                                                    
     |████████████████████████████████| 246 kB 2.4 MB/s                                    
Collecting hupper>=1.5                                                                                                                                                                 
  Using cached hupper-1.10.3-py2.py3-none-any.whl (26 kB)                                                                                                                              
Requirement already satisfied: setuptools in ./.venv/lib/python3.8/site-packages (from pyramid) (44.0.0)
Collecting webob>=1.8.3                                                                                                                                                                
  Using cached WebOb-1.8.7-py2.py3-none-any.whl (114 kB)                                                                                                                               
Collecting zope.interface>=3.8.0                                                                                                                                                       
  Using cached zope.interface-5.4.0-cp38-cp38-manylinux2010_x86_64.whl (259 kB)                                                                                                        
Collecting plaster-pastedeploy                                                                                                                                                         
  Using cached plaster_pastedeploy-0.7-py2.py3-none-any.whl (7.8 kB)                                                                                                                   
Collecting translationstring>=0.4                                                          
  Using cached translationstring-1.4-py2.py3-none-any.whl (15 kB)       
Collecting venusian>=1.0                                                                                                                                                               
  Using cached venusian-3.0.0-py3-none-any.whl (13 kB)                                                                                                                                 
Collecting plaster
  Using cached plaster-1.0-py2.py3-none-any.whl (14 kB)
Collecting zope.deprecation>=3.5.0
  Using cached zope.deprecation-4.4.0-py2.py3-none-any.whl (10 kB)
Collecting PasteDeploy>=2.0
  Using cached PasteDeploy-2.1.1-py2.py3-none-any.whl (17 kB)
Installing collected packages: hupper, webob, zope.interface, PasteDeploy, plaster, plaster-pastedeploy, translationstring, venusian, zope.deprecation, pyramid
Successfully installed PasteDeploy-2.1.1 hupper-1.10.3 plaster-1.0 plaster-pastedeploy-0.7 pyramid-2.0 translationstring-1.4 venusian-3.0.0 webob-1.8.7 zope.deprecation-4.4.0 zope.int
erface-5.4.0
Enter fullscreen mode Exit fullscreen mode

Code

I use a slightly modified example from the documentation. server.py:

from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response


def hello_world(request):
    return Response('Hello World!')


if __name__ == '__main__':
    HOST = '0.0.0.0'
    PORT = 6543

    with Configurator() as config:
        config.add_route('hello', '/')
        config.add_view(hello_world, route_name='hello')
        app = config.make_wsgi_app()
    server = make_server(HOST, PORT, app)
    print(f'Starting server at {HOST}:{PORT}') 
    server.serve_forever()
Enter fullscreen mode Exit fullscreen mode

I moved HOST & PORT into variables and logged them to the console. Otherwise, the server was quietly working without notifying the user about its presence.

Running server

(.venv) $ python3 server.py
Starting server at 0.0.0.0:6543
Enter fullscreen mode Exit fullscreen mode

The server works as expected:

pyramid-hello-world.png

Links

Summary

We have seen how to start a simple Pyramid server.

Discussion (0)