Skip to content

Instantly share code, notes, and snippets.

@takegue
Last active Jan 14, 2021
Embed
What would you like to do?
Simple DSP Server for AdTech Competition 2016. To run this code, you have to install "tornado" which is a Python Framework.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from tornado import ioloop, httpserver, web, httpclient
import json
import urllib.parse
import logging
import time
logger = logging.getLogger(__name__)
port = 8888
sample_request = {
"id" : "auction_id",
"floorPrice" : 30.3,
"site" : "media_20",
"device" : "iOS20",
"user" : "user_id",
"test" : 0
}
class MainHandler(web.RequestHandler):
def get(self):
self.write("Hello, world")
class DSPResponseHandler(web.RequestHandler):
def initilize(self):
"""
databaseの接続等を行う
"""
pass
def get(self):
self.write("Hello, world. Response Endpoint")
@web.asynchronous
def post(self):
self.set_header("Content-Type", "application/json")
data = self.request.body
response = self._response(data)
if response:
self.set_status(200)
self.write(urllib.parse.urlencode(response))
else:
self.set_status(400)
self.finish()
def _response(self, info):
""" ロジック :
やりとりされる単位は 全てCPM
Return response
None または 空の {}を返すと, Status: 204 を返す
{} を返すと, Status: 204 を返す
responseのフォーマット 例:
{
"id" : "auction_id",
"bidPrice" : 30.3,
"advertiserId" : "5"
}
"""
response = {
"id": "auction_id",
"bidPrice": 30.3,
"advertiserId": "5"
}
return response
class DSPWinNoticeHandler(web.RequestHandler):
def get(self):
self.write("Hello, world: WinNoticeEndpoint")
@web.asynchronous
def post(self):
self.set_header("Content-Type", "application/json")
data = self.request.body
self._win_notice(data)
self.write('Received WinNoticed')
self.set_status(200)
self.finish()
def _win_notice(self, info):
""" ロジック
info:Dict型 winNotice が返ってくる(id, price, isClick)
この関数をオーバーライドして, いろいろ作ってくれ
やりとりされる単位は 全てCPC
"""
pass
def make_app():
return web.Application([
(r"/", MainHandler),
(r"/dsp", DSPResponseHandler),
(r"/win_notice", DSPWinNoticeHandler),
])
if __name__ == "__main__":
app = httpserver.HTTPServer(make_app())
app.bind(port)
app.start(0) # CPUを全て使う
ioloop.IOLoop.current().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment