Created
March 24, 2015 11:34
-
-
Save kalloc/a2a4f5e6f73aa1c0ef4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import random | |
from tornado import httpclient | |
from tornado import ( | |
web, ioloop, escape, httpserver, options, gen | |
) | |
import logging | |
logging.getLogger("tornado.access").propagate = False | |
options.define("ports", default='9000, 9001, 9002', | |
help="run on the given port") | |
options.define("debug", default=False, help="run in debug mode") | |
options.parse_command_line() | |
QUERY_TPL = '?source={site_id}&price={win_price}&url_google=%%CLICK_URL_ESC%%' | |
URLS = [ | |
'http://mail.ru/' + QUERY_TPL, | |
'http://ya.ru/' + QUERY_TPL, | |
'http://yandex.ru/' + QUERY_TPL | |
] | |
# URLS = [ | |
# 'http://mail.ru/', | |
# 'http://ya.ru/', | |
# 'http://yandex.ru/' | |
# ] | |
# URLS = [item + QUERY_TPL for item in URLS] | |
class MainHandler(web.RequestHandler): | |
def post(self): | |
if random.randint(0, 4) == 1: | |
raise web.HTTPError(500) | |
if random.randint(0, 4) == 1: | |
raise web.HTTPError(204) | |
params = escape.json_decode(self.request.body) | |
print(params) | |
response = { | |
'id': params['id'], | |
'cur': 'RUB', | |
'seatbid': [{ | |
'bid': [{ | |
'id': random.randint(0, 2 ** 10), | |
'adm': '<img src="http://ya.ru/?url=%%CLICK_URL_ESC%%" />', | |
'ext': dict(url=random.choice(URLS)), | |
'price': random.randint(1, 100), | |
'impid': params['imp'][0]['id'] | |
}] | |
}] | |
} | |
self.write(response) | |
@web.asynchronous | |
def get(self): | |
response = { | |
'id': '111', | |
'cur': 'RUB', | |
'seatbid': [{ | |
'bid': [{ | |
'id': '111', | |
'adm': '<img src="http://ya.ru/?url=%%CLICK_URL_ESC%%" />', | |
'ext': dict(url='http://ya.ru'), | |
'price': '111', | |
'impid': '111' | |
}] | |
}] | |
} | |
self.write(response) | |
self.finish() | |
class MainTestHandler(web.RequestHandler): | |
def get(self): | |
self.write('test') | |
self.finish() | |
class MainSubHandler(web.RequestHandler): | |
max_fd = 0 | |
@gen.coroutine | |
def get(self): | |
cur_fd = self.request.connection.stream.socket.fileno() | |
if MainSubHandler.max_fd < cur_fd: | |
MainSubHandler.max_fd = cur_fd | |
futures = [] | |
for _ in xrange(5): | |
future = httpclient.AsyncHTTPClient().fetch( | |
'http://127.0.0.1:4001/version', method='GET', | |
headers={'Content-Type': 'application/json; charset=UTF-8'}, | |
request_timeout=10, raise_error=False | |
) | |
futures.append(future) | |
yield futures | |
id_ = 0 | |
for future in futures: | |
id_ += int(future.result().code) | |
response = { | |
'id': id_, | |
'cur': 'RUB', | |
'seatbid': [{ | |
'bid': [{ | |
'id': MainSubHandler.max_fd, | |
'adm': '<img src="http://ya.ru/?url=%%CLICK_URL_ESC%%" />', | |
'ext': dict(url='http://ya.ru'), | |
'price': '111', | |
'impid': '111' | |
}] | |
}] | |
} | |
self.write(response) | |
self.finish() | |
application = web.Application([ | |
(r"/", MainHandler), | |
(r"/with_subrequest", MainSubHandler), | |
(r"/test", MainTestHandler), | |
], debug=options.options.debug) | |
if __name__ == "__main__": | |
httpclient.AsyncHTTPClient.configure( | |
"tornado.curl_httpclient.CurlAsyncHTTPClient", max_clients=1000 | |
) | |
server = httpserver.HTTPServer(application, xheaders=True) | |
map(server.bind, options.options.ports.split(',')) | |
num_processes = 1 if options.options.debug else 0 | |
server.start(num_processes=num_processes) | |
ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment