Commit ee02f946 authored by Ivar Refsdal's avatar Ivar Refsdal

...

parent e7c6092c
#!/bin/sh
uwsgi --plugin python \
--http-socket :8090 \
--wsgi-file saveserver.py
\ No newline at end of file
import time
import werkzeug
import pprint
import tempfile
import re
import codecs
from io import BytesIO
def current_milli_time():
return int(round(time.time() * 1000))
......@@ -34,22 +39,45 @@ def application(environ, start_response):
response_headers = [
('Content-Type', 'text/plain'),
]
content_length = werkzeug.wsgi.get_content_length(environ)
print("content_length is", content_length)
if content_length != None:
ms = measure_spent_time()
print("saving content ... Woof.")
inp = environ['wsgi.input']
with open('tmp.bin', 'wb') as wfd:
while content_length>0:
size = 1024*64
if size>content_length:
size = content_length
dat = inp.read(size)
wfd.write(dat)
content_length -= len(dat)
print("so we are done => " + ms())
pprint.pprint(dir(environ))
pprint.pprint(environ)
def factory(total_content_length, filename, content_type,
content_length=None):
"""The stream factory that is used per default."""
print("total_content_length", total_content_length)
print("filename is", filename)
print("content_type", content_type)
print("*"*80)
return tempfile.TemporaryFile('wb+')
if environ.get('REQUEST_METHOD', 'GET') == 'POST':
print("hello post")
#(stream, form, files) = werkzeug.formparser.parse_form_data(environ, factory)
content_type = environ.get('CONTENT_TYPE', '')
content_length = werkzeug.formparser.get_content_length(environ)
mimetype, options = werkzeug.formparser.parse_options_header(content_type)
print(mimetype)
print(options)
parser = MultiPartToDiskWithExtension()
parser.parse(werkzeug.formparser.get_input_stream(environ), options.get('boundary'), content_length)
# content_length = werkzeug.wsgi.get_content_length(environ)
# print("content_length is", content_length)
# if content_length != None:
# ms = measure_spent_time()
# print("saving content ... Woof.")
# inp = environ['wsgi.input']
# with open('tmp.bin', 'wb') as wfd:
# while content_length>0:
# size = 1024*64
# if size>content_length:
# size = content_length
# dat = inp.read(size)
# wfd.write(dat)
# content_length -= len(dat)
# print("so we are done => " + ms())
start_response(status, response_headers)
return [response_body]
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import multiprocessing
import os
import re
import sys
import time
import uuid
from logging import StreamHandler
import gunicorn.app.base
from flask import Flask, Response, request
from gunicorn.six import iteritems
class StandaloneApplication(gunicorn.app.base.BaseApplication):
def __init__(self, app, options=None):
self.options = options or {}
self.application = app
super(StandaloneApplication, self).__init__()
def load_config(self):
config = dict([(key, value) for key, value in iteritems(self.options)
if key in self.cfg.settings and value is not None])
for key, value in iteritems(config):
self.cfg.set(key.lower(), value)
def load(self):
return self.application
def current_milli_time():
return int(round(time.time() * 1000))
def intWithCommas(x):
if type(x) not in [type(0), type(0L)]:
raise TypeError("Parameter must be an integer.")
if x < 0:
return '-' + intWithCommas(-x)
result = ''
while x >= 1000:
x, r = divmod(x, 1000)
result = ",%03d%s" % (r, result)
return "%d%s" % (x, result)
def measure_spent_time():
start = current_milli_time()
diff = { 'res' : None }
def get_spent_time(raw=False):
if diff['res'] == None:
diff['res'] = current_milli_time() - start
if raw:
return diff['res']
else:
return intWithCommas(diff['res'])
return get_spent_time
def get_app():
app = Flask(__name__)
@app.route('/')
def show_ui():
return 'hello world'
@app.route('/v0/convert', methods = ['POST'])
def convert_endpoint():
app.logger.info("received POST ... :-)")
ms = measure_spent_time()
with open('meh.bin', 'wb') as fd:
s = request.stream
bytes_written = 0
while True:
buf = s.read(1024*16)
if buf:
fd.write(buf)
bytes_written+= len(buf)
if (bytes_written % (10*1024*1024)) == 0:
app.logger.info("wrote %d megabytes...", bytes_written / (1024*1024))
else:
break
app.logger.info("got request ... => " + ms() + " ms")
app.logger.info("" + str(request.headers))
app.logger.info("" + str(request.args))
return 'ok'
return app
if __name__ == '__main__':
app = get_app()
options = {
'workers' : 2, #(multiprocessing.cpu_count() * 2) + 1,
'timeout': 60*25, # 25 minutes timeout for worker process
'logconfig' : './gunicorn_logging.conf',
'bind' : '0.0.0.0:8080'
}
StandaloneApplication(app, options).run()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment