lang/py

python helloWorld

C/H 2018. 6. 11. 08:30

Python

#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer

PORT_NUMBER = 1337

class myHandler(BaseHTTPRequestHandler):
	
	#Handler for the GET requests
	def do_GET(self):
		self.send_response(200)
		self.send_header('Content-type','text/html')
		self.end_headers()
		# Send the html message
		self.wfile.write("Hello World !")
		return

try:
	#Create a web server and define the handler to manage the
	#incoming request
	server = HTTPServer(('', PORT_NUMBER), myHandler)
	print 'Started httpserver on port ' , PORT_NUMBER
	
	#Wait forever for incoming htto requests
	server.serve_forever()

except KeyboardInterrupt:
	print '^C received, shutting down the web server'
	server.socket.close()
python serve.py

# ohter terminal
ab -n 10000 -c 10 -k http://127.0.0.1:1337/
.....
Concurrency Level:      10
Time taken for tests:   1.554 seconds
Complete requests:      10000
Failed requests:        0
Keep-Alive requests:    0
Total transferred:      1330000 bytes
HTML transferred:       130000 bytes
Requests per second:    6435.53 [#/sec] (mean)
Time per request:       1.554 [ms] (mean)
Time per request:       0.155 [ms] (mean, across all concurrent requests)
Transfer rate:          835.86 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0  20.4      0    1026
Processing:     0    1   4.6      1     415
Waiting:        0    1   4.6      1     415
Total:          0    1  23.8      1    1442

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      2
  98%      3
  99%      3
 100%   1442 (longest request)

Python bottle

sudo apt-get install python-pip
sudo pip install bottle
sudo apt-get install gunicorn
mkdir webapp
cd webapp
vi app.py
from bottle import Bottle, run

app = Bottle()

@app.route('/')
@app.route('/hello')
def hello():
   return "Hello World!"

#run(app, host = 'localhost', port = 1337)
run(server = 'gunicorn', host='localhost', port=1337
python app.py
Bottle v0.12.13 server starting up (using WSGIRefServer())...
...

# other sell
ab -n10000 -c10 -g out.data -k http://127.0.0.1:1337/
...
Concurrency Level:      10
Time taken for tests:   2.736 seconds
Complete requests:      10000
Failed requests:        0
Non-2xx responses:      10000
Keep-Alive requests:    0
Total transferred:      8880000 bytes
HTML transferred:       7200000 bytes
Requests per second:    3654.52 [#/sec] (mean)
Time per request:       2.736 [ms] (mean)
Time per request:       0.274 [ms] (mean, across all concurrent requests)
Transfer rate:          3169.15 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     2    3   0.4      3       9
Waiting:        1    3   0.4      3       9
Total:          2    3   0.4      3       9

Percentage of the requests served within a certain time (ms)
  50%      3
  66%      3
  75%      3
  80%      3
  90%      3
  95%      3
  98%      4
  99%      5
 100%      9 (longest request)



반응형

'lang > py' 카테고리의 다른 글

python3 를 기본값으로 설정  (0) 2018.08.11
Python Scrapy 사이트 스크랩/크롤링  (0) 2018.06.28
파이썬 라이브러리 가상환경 묶기  (0) 2017.05.16
Python 정규표현식 re  (0) 2016.12.12
python3 format() 서식화 메서드  (0) 2016.12.08