Docker学习笔记:Alpine镜像+Python3安装+http服务器
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Docker学习笔记:Alpine镜像+Python3安装+http服务器,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5072字,纯文字阅读大概需要8分钟。
内容图文
![Docker学习笔记:Alpine镜像+Python3安装+http服务器](/upload/InfoBanner/zyjiaocheng/623/28bcd77de0f5444db579e612b1ccf9ff.jpg)
- 编写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()
内容总结
以上是互联网集市为您收集整理的Docker学习笔记:Alpine镜像+Python3安装+http服务器全部内容,希望文章能够帮你解决Docker学习笔记:Alpine镜像+Python3安装+http服务器所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。