浏览代码

Started some real work around a gps tracker postions server.

tags/v1.2.0
Bertrand Lemasle 7 年前
父节点
当前提交
32be4cf9e5
共有 6 个文件被更改,包括 70 次插入25 次删除
  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 查看文件

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


+ 16
- 0
GpsTrackerServer/api.py 查看文件

@@ -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 查看文件

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

ROUTES_POSITIONS = '/positions'

+ 3
- 25
GpsTrackerServer/main.py 查看文件

@@ -2,34 +2,12 @@
Basic HTTP server to receive and save gps positions sent by Arduino GPS Tracker 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__": if __name__ == "__main__":
# Use python built-in WSGI reference implemention to run a web server # Use python built-in WSGI reference implemention to run a web server
from wsgiref.simple_server import make_server from wsgiref.simple_server import make_server


print('Starting web 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 查看文件

@@ -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 查看文件

@@ -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

正在加载...
取消
保存