- 编写Dockerfile文件使用最新的Alpine镜像并安装Python3环境,如下:
因为python高于3.4则不会默认安装pip,需要手动安装。
试了很多其他办法都没安装上,唯有下载get-pip.py进行安装。
这里说一下cherrypy版本不能高于9.0.0,负责等下import wsgiserver会出错,因为wsgiserver后面移出cherrypy了。
FROM alpine
RUN mkdir /install
COPY get-pip.py /install
RUN apk update
RUN apk add bash
RUN apk add python3
RUN python3 /install/get-pip.py
RUN pip install bottle
#RUN pip3 install cherrypy
RUN pip3 install "cherrypy>=3.0.8,<9.0.0"
ADD server.py /root/server.py
EXPOSE 8080:8080
CMD /usr/bin/python3 /root/server.py
- 在Dockerfile文件目录下执行下面命令可以创建基于python3的镜像:
$ docker build -t test_python38_http .
- 这样基于alpine的python3镜像创建成功了,用下面命令可以查看:
$ docker images
- 镜像创建成功后输入命令则可以启动镜像服务了
$ docker run -p 8080:8080 -it test_python38_http
- 最后打开浏览器,输入url测试:
http://192.168.99.100:8080/HTTP_SET_COOKIE_LIST_3
关于这个IP也是一个坑,大家可以在启动Docker时看到一个default IP,并不是其他的哦。
- 最后贴上http代码
import bottle
from bottle import post, get, delete, route, request
from cherrypy import wsgiserver
import time
import signal
@route('/HTTP_SET_COOKIE_LIST_3')
def set_cookie():
print("VA_HTTP HTTP_SET_COOKIE_LIST_3")
count = int( bottle.request.cookies.get('counter', '0') )
count += 1
bottle.response.set_cookie('counter', str(count))
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 HTTP_SET_COOKIE_LIST_3"
@route('/HTTP_SET_COOKIE_LIST_4')
def verify_cookie():
count = int( bottle.request.cookies.get('counter', '0') )
if count == 0 :
bottle.response.status = 400
bottle.response.content_type = 'text/plain; charset=utf-8'
return "400 HTTP_SET_COOKIE_LIST_4"
else :
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 HTTP_SET_COOKIE_LIST_4"
@route('/<id>')
def HttpRoute_handle(id):
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 "+ id
@route('/HTTP_TIMEOUT')
def HttpTime_handle():
time.sleep(30)
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 TIME"
#Get for Test HTTP_DO_REQUEST_1, HTTP_SET_POST_DATA_5, HTTP_SET_METHOD_3
@get('/<id>')
def HttpGet_handler(id):
if id == "HTTP_SET_METHOD_3":
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 GET"
elif (id == "HTTP_SET_HEADER_2_0") or (id == "HTTP_SET_HEADER_2_1") or (id == "HTTP_SET_HEADER_2_2"):
if(request.headers.get('FirstHeader') == 'value') and not(request.headers.get('SecondHeader')) and (id == "HTTP_SET_HEADER_2_0"):
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 HTTP_SET_HEADER_2 " + "FirstHeader"
elif (request.headers.get('FirstHeader') == 'value') and (request.headers.get('SecondHeader') == 'value') and (id == "HTTP_SET_HEADER_2_1"):
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 HTTP_SET_HEADER_2 " + "FirstHeader" + "SecondHeader"
elif not(request.headers.get('FirstHeader')) and not(request.headers.get('SecondHeader')) and( id == "HTTP_SET_HEADER_2_2"):
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 HTTP_SET_HEADER_2 "
else:
bottle.response.status = 400
bottle.response.content_type = 'text/plain; charset=utf-8'
return "400 HTTP_SET_HEADER_2"
else:
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 "+ id
#Post for Test : HTTP_SET_POST_DATA_3, HTTP_SET_POST_DATA_4, HTTP_SET_POST_DATA_5, HTTP_SET_METHOD_3
@post('/<id>')
def HttpSetPostData_handler(id):
if id == "HTTP_SET_POST_DATA_5" :
bottle.response.status = 400
bottle.response.content_type = 'text/plain; charset=utf-8'
return "400 " + id
elif id == "HTTP_SET_METHOD_3":
Data = bottle.request.forms.get('data')
Cata = bottle.request.forms.get('cata')
if Data:
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 POST " + "data=" + Data
elif Cata:
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 POST " + "cata=" + Cata
else:
bottle.response.status = 400
bottle.response.content_type = 'text/plain; charset=utf-8'
return "400 " + id
else:
testName = bottle.request.forms.get('testname')
if testName == "HTTP_SET_POST_DATA_4":
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 " + id
elif testName == id:
#HTTP_SET_POST_DATA_3
bottle.response.status = 200
bottle.response.content_type = 'text/plain; charset=utf-8'
return "200 " + testName
else :
bottle.response.status = 400
bottle.response.content_type = 'text/plain; charset=utf-8'
return "400 " + id
def stop_server(*args, **kwargs):
server.stop()
app = bottle.default_app()
server = wsgiserver.CherryPyWSGIServer(("0.0.0.0", 8080), app)
signal.signal(signal.SIGINT, stop_server)
signal.signal(signal.SIGTERM, stop_server)
signal.signal(signal.SIGHUP, stop_server)
print("VA_HTTP TestSuite Server Started")
server.start()