Selaa lähdekoodia

Started some real work around a gps tracker postions server.

tags/v1.2.0
Bertrand Lemasle 7 vuotta sitten
vanhempi
commit
32be4cf9e5
6 muutettua tiedostoa jossa 70 lisäystä ja 25 poistoa
  1. +12
    -0
      GpsTrackerServer/GpsTrackerServer.pyproj
  2. +16
    -0
      GpsTrackerServer/api.py
  3. +4
    -0
      GpsTrackerServer/constants.py
  4. +3
    -25
      GpsTrackerServer/main.py
  5. +24
    -0
      GpsTrackerServer/middlewares.py
  6. +11
    -0
      GpsTrackerServer/resources.py

+ 12
- 0
GpsTrackerServer/GpsTrackerServer.pyproj Näytä tiedosto

@@ -27,9 +27,21 @@
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<ItemGroup>
<Compile Include="api.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="constants.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="main.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="middlewares.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="resources.py">
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<InterpreterReference Include="Global|PythonCore|3.6" />


+ 16
- 0
GpsTrackerServer/api.py Näytä tiedosto

@@ -0,0 +1,16 @@
import falcon
import middlewares
import resources
import constants

#from constants import *

def create():
api = falcon.API(media_type=constants.MEDIA_CSV, middleware=[
middlewares.AuthMiddleware(constants.USER_AGENT),
middlewares.RequireCSV()
])

api.add_route(constants.ROUTES_POSITIONS, resources.PositionsResource())

return api

+ 4
- 0
GpsTrackerServer/constants.py Näytä tiedosto

@@ -0,0 +1,4 @@
USER_AGENT = 'SIMCOM_MODULE'
MEDIA_CSV = 'text/csv'

ROUTES_POSITIONS = '/positions'

+ 3
- 25
GpsTrackerServer/main.py Näytä tiedosto

@@ -2,34 +2,12 @@
Basic HTTP server to receive and save gps positions sent by Arduino GPS Tracker
"""

import falcon

class RequireCSV:

def process_request(self, req, resp):
if req.method in ('POST', 'PUT'):
if 'text/csv' not in req.content_type:
raise falcon.HTTPUnsupportedMediaType('Only CSV is supported')

class PositionsResource:

def on_post(self, req, resp):
data = req.bounded_stream.read()
resp.body = data
resp.status = falcon.HTTP_201

positions = PositionsResource()

api = falcon.API(middleware=[
RequireCSV()
])

api.add_route('/positions', positions)
import api

if __name__ == "__main__":
# Use python built-in WSGI reference implemention to run a web server
from wsgiref.simple_server import make_server

print('Starting web server...')
srv = make_server('localhost', 8080, api)
srv.serve_forever()
srv = make_server('localhost', 8080, api.create())
srv.serve_forever()

+ 24
- 0
GpsTrackerServer/middlewares.py Näytä tiedosto

@@ -0,0 +1,24 @@
import falcon
import constants

class RequireCSV(object):

def process_request(self, req, resp):
if req.method in ('POST', 'PUT'):
if constants.MEDIA_CSV not in req.content_type:
raise falcon.HTTPUnsupportedMediaType

class AuthMiddleware(object):

def __init__(self, userAgentRequired):
self._userAgentRequired = userAgentRequired

def process_request(self, req, resp):
authorization = req.get_header('Authorization')
userAgent = req.get_header('User-Agent')

if userAgent != self._userAgentRequired:
if authorization is None: #make clients believe that Authorization header is the issue
raise falcon.HTTPForbidden
else:
raise falcon.HTTPUnauthorized

+ 11
- 0
GpsTrackerServer/resources.py Näytä tiedosto

@@ -0,0 +1,11 @@
import falcon

class PositionsResource:

def on_post(self, req, resp):
data = req.bounded_stream.read()
resp.body = data
resp.status = falcon.HTTP_201

def on_get(self, req, resp):
raise falcon.HTTPNotImplemented

Ladataan…
Peruuta
Tallenna