将一个简单的python程序打包成加密的docker镜像并对外提供接口
https://blog.csdn.net/uncle_yiba/article/details/80046157环境:python2.7docker:一、一个简单的python程序既然是一个简单的python程序,那我们就实现一个简单的加法功能即可。#coding=utf-8import randomdef add(aStr,bStr):map={}try:a=float(aStr)b=floa
https://blog.csdn.net/uncle_yiba/article/details/80046157
环境:python2.7
docker:
一、一个简单的python程序
既然是一个简单的python程序,那我们就实现一个简单的加法功能即可。add.py,新建data文件夹
#coding=utf-8
import random
def add(aStr,bStr):
map={}
try:
a=float(aStr)
b=float(bStr)
sum=a+b
file_name=str(int(random.random()*1000000))+""
file=open("data/"+file_name+'.txt',"w")
file.write(str(a)+"+"+str(b)+"="+str(sum))
file.close()
map["result"]="OK"
map["id"]=str(file_name)
except:
map["result"]="wrong"
return map
if __name__=="__main__":
print "运行run啊大哥,不是这个"
map=add("4.5",5.6)
print map
这个方法实现的功能都能看明白,就是把a和b相加,并且将结果输出在一个文件中,并返回是否运行成功,如果成功则再加上文件的编号(后面那段不管= =)
二、对外提供一个调用接口
除了这段代码我们还需要对外提供一个调用接口,我使用tornado开一个web服务来提供接口,这个方法命名为run方法= =
#coding=utf-8
import tornado.web
import tornado.ioloop
from add import add
#定义处理类
class Add(tornado.web.RequestHandler):
#添加一个post请求方式的方法
def post(self):
#向响应中,添加数据
a=self.get_argument('a', '')
b=self.get_argument('b', '')
map=add(a,b)
self.write(map)
def make_app():
return tornado.web.Application(
[(r'/add', Add)],
)
if __name__ == '__main__':
# 创建一个应用对象
app = make_app()
#绑定一个监听端口
app.listen(8778)
#启动web程序,开始监听端口的连接
tornado.ioloop.IOLoop.current().start()
当我们运行run.py之后,在终端输入命令 curl -d 'a=3&b=9' 'localhost:8778/add'
终端返回信息为:{"result": "OK", "id": "808992"} (这里的ID不一定会相同,只是一个随机数)
在到同文件下面的data目录即可找到对应的数据文件
到此,我们已经实现了一个python的简单程序,并使其对外提供了一个接口调用,现在我们要将其打包到docker镜像中并对关键代码(add)进行加密
三、运行一个ubuntu镜像,对安装好python,vim等以及必要的python包例如tornado(docker 的安装使用就不多说了= =)
显然我们需要的是第一个镜像,将其pull到本地,即运行docker pull ubuntu
查看本地镜像docker images 可以看到刚才的镜像
启动它!并安装一些必要的东西!
docker run -it c9d990395902 /bin/bash 其中c9d990395902为ubuntu的镜像ID,如果你pull的时候它已经更新过了ID可能产生变化,/bin/bash是使得进入新运行的容器内部
首先执行apt update更新ubuntu系统
然后使用apt install python安装python2
中途会问你有一步是否继续,输入y并回车即可
输入python能进入如下页面即安装成功,输入exit()回车退出编辑
使用apt-get install python-pip python-dev build-essential安装pip,注意同样需要y回车
无法安装pip
apt-get install python-pip
修改为apt-get install python3-pip就可以............
使用pip3
python3
安装完成后输入pip -V即可查看是否安装成功
安装vim,命令是apt-get install vim
导入一些python的包,比如tornado,使用命令pip install tornado
再比如cython,命令为pip install cython
至此,我们给这个容器安装完成了python,pip,vim以及导入了两个必要的包
四、新建文件夹并且把代码复制到容器中
在根目录运行mkdir add_uncleyiba,新建文件夹add_uncleyiba
进入add_uncleyiba文件夹,新建目录data,新建文件test.py
用vim打开test.py
随便按个键比如a,进入编辑模式,输入print 123,然后按Esc,输入:wq(前面是冒号),然后回车,即为保存
运行python test.py,测试一下没有问题
现在新建三个文件,分别叫add.py,run.py,setup.py
文件夹data也要..............
利用vim打开add.py把之前的代码复制进去,同理复制run.py的代码(注意,先进入编辑模式之后再复制)
复制并保存之后可以利用cat命令查看代码是否有问题
cat add.py
我们可以尝试在容器中运行add.py,使用命令python add.py
看上去也没有问题
现在vim打开setup.py文件,并输入以下内容
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize(["add.py"]))
然后保存退出
运行命令python setup.py build_ext,目的是为了生成add.py文件的so文件,保证源码安全
我们可以发现新生成了build文件夹
如图add.so即我们需要的so文件!
使用mv add.so ../../add.so将它放到外层的文件夹去
现在我们可以删除一些没有用的东西了,除了add.so,data,run.py,setup.py,其他的都可以删除了
友情提示:删除命令小心使用= =动图想必都看过了
图片好像动不起来- -可以去一些水群里面找图。。。
五、提交镜像,并运行测试
重新打开一个终端,然后将该容器制作成一个镜像docker commit 248224b7067e add_uncleyiba:1.0
id 是查看自己的容器id得到的,后面的是你要生成的镜像名字+冒号+版本号
之后我们查看本地镜像的时候就会发现成圣了add_uncleyiba:1.0这个镜像了
本机里的,回到一个任意目录,当然,最好是你之前保存python代码的目录,新建文件夹名字叫data
然后运行命令
docker run -itd -p 8899:8888 -v #{local_abs_path}:/add_uncleyiba/data add_uncleyiba:1.0 python /add_uncleyiba/run.py
docker run -itd -p 8899:8778 -v/home/hlx2/wieghts/test/data:/add_uncleyiba/data add_uncleyiba:1.0 python /add_uncleyiba/run.py
(base) root@NLP:/home/hlx2/wieghts/test# docker run -itd -p 8899:8778 -v/home/hlx2/wieghts/test/data:/add_uncleyiba/data add_uncleyiba:1.0 python /add_uncleyiba/run.py
/home/hlx2/wieghts/test
其中#{local_abs_path}改成data文件夹的本地的绝对路径(cd进目录后使用pwd查看)
出现新的容器ID后使用curl -d 'a=5&b=7' 'localhost:8888/add'进行访问测试就好了~
curl -d 'a=5&b=7' 'localhost:8899/add'
(base) root@NLP:/home/hlx2/wieghts/test# curl -d 'a=5&b=7' 'localhost:8899/add'
{"result": "wrong"}
(base) root@NLP:/home/hlx2/wieghts/test# curl -d 'a=5&b=7' 'localhost:8899/add'
这时候我们可以进入到本地的data文件夹,会发现里面有一个文件名就是刚才返回的id值
打开即为我们刚才的测试内容
hlx2@NLP:~/wieghts/test$ curl -d 'a=3&b=9' 'localhost:8778/add'
{"result": "wrong"}hlx2@NLP:~/wieghts/test$ curl
curl: try 'curl --help' or 'curl --manual' for more information
hlx2@NLP:~/wieghts/test$ curl -d 'a=3&b=9' 'localhost:8778/add'
{"result": "wrong"}hlx2@NLP:~/wieghts/test$ curl -d 'a=3&b=9' 'localhost:8778/add'
{"result": "OK", "id": "711153"}hlx2@NLP:~/wieghts/test$ docker search ubuntu
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/images/search?limit=25&term=ubuntu: dial unix /var/run/docker.sock: connect: permission denied
hlx2@NLP:~/wieghts/test$ su root
密码:
(base) root@NLP:/home/hlx2/wieghts/test# docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 11666 [OK]
dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 482 [OK]
websphere-liberty WebSphere Liberty multi-architecture images … 265 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 251 [OK]
consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 229 [OK]
ubuntu-upstart Upstart is an event-based replacement for th… 109 [OK]
pivotaldata/ubuntu A quick freshening-up of the base Ubuntu doc… 4
pivotaldata/ubuntu16.04-build Ubuntu 16.04 image for GPDB compilation 2
smartentry/ubuntu ubuntu with smartentry 1 [OK]
1and1internet/ubuntu-16-php-7.1 ubuntu-16-php-7.1 1 [OK]
1and1internet/ubuntu-16-sshd ubuntu-16-sshd 1 [OK]
pivotaldata/ubuntu-gpdb-dev Ubuntu images for GPDB development 1
(base) root@NLP:/home/hlx2/wieghts/test# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
da7391352a9b: Pull complete
14428a6d4bcd: Pull complete
2c2d948710f2: Pull complete
Digest: sha256:c95a8e48bf88e9849f3e0f723d9f49fa12c5a00cfc6e60d2bc99d87555295e4c
Status: Downloaded newer image for ubuntu:latest
(base) root@NLP:/home/hlx2/wieghts/test# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nvidia/cuda 10.0-cudnn7-runtime-ubuntu16.04-my_1 d83d5fbb8f13 11 days ago 1.57GB
trwebocr latest 2d52c2f0e01b 2 weeks ago 1.15GB
training/webapp latest 6fae60ef3446 5 years ago 349MB
(base) root@NLP:/home/hlx2/wieghts/test# docker images|grep ubuntu
nvidia/cuda 10.0-cudnn7-runtime-ubuntu16.04-my_1 d83d5fbb8f13 11 days ago 1.57GB
nvidia/cuda 10.0-cudnn7-runtime-ubuntu16.04-my ecb1ddc85fb9 3 weeks ago 3.64GB
ubuntu latest f643c72bc252 4 weeks ago 72.9MB
nvidia/cuda 10.0-cudnn7-runtime-ubuntu16.04 a00b882cd4bc 2 months ago 1.33GB
ubuntu <none> 1d622ef86b13 8 months ago 73.9MB
ubuntu 14.04 6e4f1fe62ff1 12 months ago 197MB
ubuntu 15.10 9b9cb95443b5 4 years ago 137MB
(base) root@NLP:/home/hlx2/wieghts/test# docker run -it f643c72bc252 /bin/bash
root@305d47e07414:/# apt update
Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [109 kB]
Get:2 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [645 kB]
Reading package lists... Done
Building dependency tree
Reading state information... Done
2 packages can be upgraded. Run 'apt list --upgradable' to see them.
root@305d47e07414:/# apt install python
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'python-is-python2' instead of 'python'
...........
Setting up python2.7 (2.7.18-1~20.04) ...
Setting up libpython2-stdlib:amd64 (2.7.17-2ubuntu4) ...
Setting up python2 (2.7.17-2ubuntu4) ...
Setting up python-is-python2 (2.7.17-4) ...
Processing triggers for libc-bin (2.31-0ubuntu9.1) ...
root@305d47e07414:/# python
Python 2.7.18 (default, Aug 4 2020, 11:16:42)
[GCC 9.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
root@305d47e07414:/# apt-get install python-pip python-dev build-essential
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'python-dev-is-python2' instead of 'python-dev'
E: Unable to locate package python-pip
root@305d47e07414:/# pip
bash: pip: command not found
root@305d47e07414:/# apt-get install pip
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package pip
root@305d47e07414:/# apt-get install python-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'python-dev-is-python2' instead of 'python-dev'
..........................
Setting up python2-dev (2.7.17-2ubuntu4) ...
Setting up python-dev-is-python2 (2.7.17-4) ...
Processing triggers for libc-bin (2.31-0ubuntu9.1) ...
root@305d47e07414:/# pip
bash: pip: command not found
root@305d47e07414:/# apt update
Hit:1 http://security.ubuntu.com/ubuntu focal-security InRelease
Err:2 http://archive.ubuntu.com/ubuntu focal InRelease
Temporary failure resolving 'archive.ubuntu.com'
................................
root@305d47e07414:/# apt install python-pip
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package python-pip
root@305d47e07414:/# apt update
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Hit:4 http://security.ubuntu.com/ubuntu focal-security InRelease
Reading package lists... Done
Building dependency tree
Reading state information... Done
2 packages can be upgraded. Run 'apt list --upgradable' to see them.
root@305d47e07414:/# apt-get install python-pip
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package python-pip
root@305d47e07414:/# apt-get install python3-pip
Reading package lists... Done
Building dependency tree
...................................................
Processing triggers for mime-support (3.64ubuntu1) ...
Processing triggers for ca-certificates (20201027ubuntu0.20.04.1) ...
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
root@305d47e07414:/# python
Python 2.7.18 (default, Aug 4 2020, 11:16:42)
[GCC 9.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> qiut()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'qiut' is not defined
>>> quit()
root@305d47e07414:/# pip
bash: pip: command not found
root@305d47e07414:/# pip3Usage:
pip3 <command> [options]Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
config Manage local and global configuration.
search Search PyPI for packages.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
debug Show information useful for debugging.
help Show help for commands.General Options:
-h, --help Show help.
--isolated Run pip in an isolated mode, ignoring
environment variables and user configuration.
-v, --verbose Give more output. Option is additive, and can be
used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output. Option is additive, and can be
used up to 3 times (corresponding to WARNING,
ERROR, and CRITICAL logging levels).
--log <path> Path to a verbose appending log.
--proxy <proxy> Specify a proxy in the form
[user:passwd@]proxy.server:port.
--retries <retries> Maximum number of retries each connection should
attempt (default 5 times).
--timeout <sec> Set the socket timeout (default 15 seconds).
--exists-action <action> Default action when a path already exists:
(s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
--trusted-host <hostname> Mark this host or host:port pair as trusted,
even though it does not have valid or any HTTPS.
--cert <path> Path to alternate CA bundle.
--client-cert <path> Path to SSL client certificate, a single file
containing the private key and the certificate
in PEM format.
--cache-dir <dir> Store the cache data in <dir>.
--no-cache-dir Disable the cache.
--disable-pip-version-check
Don't periodically check PyPI to determine
whether a new version of pip is available for
download. Implied with --no-index.
--no-color Suppress colored output
--no-python-version-warning
Silence deprecation warnings for upcoming
unsupported Pythons.
root@305d47e07414:/# python3
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
root@305d47e07414:/# apt-get install vim
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
alsa-topology-conf alsa-ucm-conf libasound2 libasound2-data libcanberra0
libgpm2 libltdl7 libogg0 libtdb1 libvorbis0a libvorbisfile3
sound-theme-freedesktop vim-common vim-runtime xxd
Suggested packages:
libasound2-plugins alsa-utils libcanberra-gtk0 libcanberra-pulse gpm ctags
vim-doc vim-scripts
The following NEW packages will be installed:
alsa-topology-conf alsa-ucm-conf libasound2 libasound2-data libcanberra0
libgpm2 libltdl7 libogg0 libtdb1 libvorbis0a libvorbisfile3
sound-theme-freedesktop vim vim-common vim-runtime xxd
0 upgraded, 16 newly installed, 0 to remove and 2 not upgraded.
Need to get 8281 kB of archives.
After this operation, 38.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 xxd amd64 2:8.1.2269-1ubuntu5 [50.1 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal/main amd64 vim-common all 2:8.1.2269-1ubuntu5 [85.1 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal/main amd64 alsa-topology-conf all 1.2.2-1 [7364 B]
Get:4 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 alsa-ucm-conf all 1.2.2-1ubuntu0.5 [25.7 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libasound2-data all 1.2.2-2.1ubuntu2.2 [20.2 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 libasound2 amd64 1.2.2-2.1ubuntu2.2 [335 kB]
Get:7 http://archive.ubuntu.com/ubuntu focal/main amd64 libltdl7 amd64 2.4.6-14 [38.5 kB]
Get:8 http://archive.ubuntu.com/ubuntu focal/main amd64 libtdb1 amd64 1.4.2-3build1 [44.1 kB]
Get:9 http://archive.ubuntu.com/ubuntu focal/main amd64 libogg0 amd64 1.3.4-0ubuntu1 [24.0 kB]
Get:10 http://archive.ubuntu.com/ubuntu focal/main amd64 libvorbis0a amd64 1.3.6-2ubuntu1 [87.0 kB]
Get:11 http://archive.ubuntu.com/ubuntu focal/main amd64 libvorbisfile3 amd64 1.3.6-2ubuntu1 [16.1 kB]
Get:12 http://archive.ubuntu.com/ubuntu focal/main amd64 sound-theme-freedesktop all 0.8-2ubuntu1 [384 kB]
Get:13 http://archive.ubuntu.com/ubuntu focal/main amd64 libcanberra0 amd64 0.30-7ubuntu1 [38.1 kB]
Get:14 http://archive.ubuntu.com/ubuntu focal/main amd64 libgpm2 amd64 1.20.7-5 [15.1 kB]
Get:15 http://archive.ubuntu.com/ubuntu focal/main amd64 vim-runtime all 2:8.1.2269-1ubuntu5 [5873 kB]
Get:16 http://archive.ubuntu.com/ubuntu focal/main amd64 vim amd64 2:8.1.2269-1ubuntu5 [1238 kB]
Fetched 8281 kB in 8s (1040 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package xxd.
(Reading database ... 15307 files and directories currently installed.)
Preparing to unpack .../00-xxd_2%3a8.1.2269-1ubuntu5_amd64.deb ...
Unpacking xxd (2:8.1.2269-1ubuntu5) ...
Selecting previously unselected package vim-common.
Preparing to unpack .../01-vim-common_2%3a8.1.2269-1ubuntu5_all.deb ...
Unpacking vim-common (2:8.1.2269-1ubuntu5) ...
Selecting previously unselected package alsa-topology-conf.
Preparing to unpack .../02-alsa-topology-conf_1.2.2-1_all.deb ...
Unpacking alsa-topology-conf (1.2.2-1) ...
Selecting previously unselected package alsa-ucm-conf.
Preparing to unpack .../03-alsa-ucm-conf_1.2.2-1ubuntu0.5_all.deb ...
Unpacking alsa-ucm-conf (1.2.2-1ubuntu0.5) ...
Selecting previously unselected package libasound2-data.
Preparing to unpack .../04-libasound2-data_1.2.2-2.1ubuntu2.2_all.deb ...
................................................
update-alternatives: warning: skip creation of /usr/share/man/man1/editor.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group editor) doesn't exist
Processing triggers for libc-bin (2.31-0ubuntu9.1) ...
Processing triggers for mime-support (3.64ubuntu1) ...
root@305d47e07414:/# pip3 install cython
Collecting cython
Downloading Cython-0.29.21-cp38-cp38-manylinux1_x86_64.whl (1.9 MB)
|████████████████████████████████| 1.9 MB 6.9 kB/s
Installing collected packages: cython
Successfully installed cython-0.29.21
root@305d47e07414:/# python3 test.py
python3: can't open file 'test.py': [Errno 2] No such file or directory
root@305d47e07414:/# vim test.py
root@305d47e07414:/# python3 test.py
123
root@305d47e07414:/#
root@305d47e07414:/# touch add.py
root@305d47e07414:/# touch run.py
root@305d47e07414:/# touch setup.py
root@305d47e07414:/# ls
add.py dev lib libx32 opt run setup.py test.py var
bin etc lib32 media proc run.py srv tmp
boot home lib64 mnt root sbin sys usr
root@305d47e07414:/# mkdir add_uncleyiba
root@305d47e07414:/# cd add_uncleyiba/'
> ^C
root@305d47e07414:/# cd add_uncleyiba
root@305d47e07414:/add_uncleyiba# touch add.py
root@305d47e07414:/add_uncleyiba# touch run.py
root@305d47e07414:/add_uncleyiba# touch setup.py
root@305d47e07414:/add_uncleyiba# ls
add.py run.py setup.py
root@305d47e07414:/add_uncleyiba# nano
bash: nano: command not found
root@305d47e07414:/add_uncleyiba# apt-get install nano
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
hunspell
The following NEW packages will be installed:
nano
0 upgraded, 1 newly installed, 0 to remove and 2 not upgraded.
Need to get 269 kB of archives.
After this operation, 868 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 nano amd64 4.8-1ubuntu1 [269 kB]
Fetched 269 kB in 13s (20.8 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package nano.
......................................
update-alternatives: warning: skip creation of /usr/share/man/man1/pico.1.gz because associated file /usr/share/man/man1/nano.1.gz (of link group pico) doesn't exist
root@305d47e07414:/add_uncleyiba# nano add.py
root@305d47e07414:/add_uncleyiba# cat add.py
#coding=utf-8
import random
def add(aStr,bStr):
map={}
try:
a=float(aStr)
b=float(bStr)
sum=a+b
file_name=str(int(random.random()*1000000))+""
file=open("data/"+file_name+'.txt',"w")
file.write(str(a)+"+"+str(b)+"="+str(sum))
file.close()
map["result"]="OK"
map["id"]=str(file_name)
except:
map["result"]="wrong"
return mapX
if __name__=="__main__":
print ("运行run啊大哥,不是这个")
res=add("4.5",5.6)
print (res)
root@305d47e07414:/add_uncleyiba#
root@305d47e07414:/add_uncleyiba# nano run.py
root@305d47e07414:/add_uncleyiba# cat run.py
#coding=utf-8
import tornado.web
import tornado.ioloop
from add import add
#定义处理类
class Add(tornado.web.RequestHandler):
#添加一个post请求方式的方法
def post(self):
#向响应中,添加数据
a=self.get_argument('a', '')
b=self.get_argument('b', '')
map=add(a,b)
self.write(map)
def make_app():
return tornado.web.Application(
[(r'/add', Add)],
)
if __name__ == '__main__':
# 创建一个应用对象
app = make_app()
#绑定一个监听端口
app.listen(8778)
#启动web程序,开始监听端口的连接
tornado.ioloop.IOLoop.current().start()
root@305d47e07414:/add_uncleyiba# python add.py
运行run啊大哥,不是这个
Traceback (most recent call last):
File "add.py", line 28, in <module>
res=add("4.5",5.6)
File "add.py", line 23, in add
return mapX
NameError: global name 'mapX' is not defined
root@305d47e07414:/add_uncleyiba# nano add.py
root@305d47e07414:/add_uncleyiba# python add.py
运行run啊大哥,不是这个
{'result': 'wrong'}
root@305d47e07414:/add_uncleyiba# python add.py
运行run啊大哥,不是这个
{'result': 'wrong'}
root@305d47e07414:/add_uncleyiba# nano add.py
root@305d47e07414:/add_uncleyiba# mkdir data
root@305d47e07414:/add_uncleyiba# python add.py
运行run啊大哥,不是这个
{'result': 'OK', 'id': '406905'}
root@305d47e07414:/add_uncleyiba# python3 run.py
Traceback (most recent call last):
File "run.py", line 3, in <module>
import tornado.web
ModuleNotFoundError: No module named 'tornado'
root@305d47e07414:/add_uncleyiba# pip3 install tornado
Collecting tornado
Downloading tornado-6.1-cp38-cp38-manylinux2010_x86_64.whl (427 kB)
|████████████████████████████████| 427 kB 63 kB/s
Installing collected packages: tornado
Successfully installed tornado-6.1
root@305d47e07414:/add_uncleyiba# pip3 install cython
Requirement already satisfied: cython in /usr/local/lib/python3.8/dist-packages (0.29.21)
root@305d47e07414:/add_uncleyiba# nano setup.py
root@305d47e07414:/add_uncleyiba# python3 setup.py build_ext
Compiling add.py because it changed.
[1/1] Cythonizing add.py
/usr/local/lib/python3.8/dist-packages/Cython/Compiler/Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /add_uncleyiba/add.py
....................
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fwrapv -O2 -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.8/add.o -o build/lib.linux-x86_64-3.8/add.cpython-38-x86_64-linux-gnu.so
root@305d47e07414:/add_uncleyiba# ls
add.c add.py build data run.py setup.py
root@305d47e07414:/add_uncleyiba# cd build
root@305d47e07414:/add_uncleyiba/build# ls
lib.linux-x86_64-3.8 temp.linux-x86_64-3.8
root@305d47e07414:/add_uncleyiba/build# cd lib.linux-x86_64-3.8/
root@305d47e07414:/add_uncleyiba/build/lib.linux-x86_64-3.8# ls
add.cpython-38-x86_64-linux-gnu.so
root@305d47e07414:/add_uncleyiba/build/lib.linux-x86_64-3.8# cp add.cpython-38-x86_64-linux-gnu.so ../../add.so
root@305d47e07414:/add_uncleyiba/build/lib.linux-x86_64-3.8# cd ..
root@305d47e07414:/add_uncleyiba/build# cd ..
root@305d47e07414:/add_uncleyiba# ls
add.c add.py add.so build data run.py setup.py
root@305d47e07414:/add_uncleyiba# ls
add.c add.py add.so build data run.py setup.py
root@305d47e07414:/add_uncleyiba# ls
add.c add.py add.so build data run.py setup.py
root@305d47e07414:/add_uncleyiba# cd data
root@305d47e07414:/add_uncleyiba/data# ls
406905.txt
root@305d47e07414:/add_uncleyiba/data# cat 406905.txt
4.5+5.6=10.1root@305d47e07414:/add_uncleyiba/data# python run
python: can't open file 'run': [Errno 2] No such file or directory
root@305d47e07414:/add_uncleyiba/data# cd ..
root@305d47e07414:/add_uncleyiba# python3 run.py
hlx2@NLP:~/wieghts/test$ docker ps -a
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/containers/json?all=1: dial unix /var/run/docker.sock: connect: permission denied
hlx2@NLP:~/wieghts/test$ su root
密码:
(base) root@NLP:/home/hlx2/wieghts/test# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
305d47e07414 f643c72bc252 "/bin/bash" 20 minutes ago Up 20 minutes mystifying_mccarthy
a2102e369d09 nvidia/cuda:10.0-cudnn7-runtime-ubuntu16.04-my_1 "/bin/bash" 11 days ago Up 11 days 0.0.0.0:9999->9999/tcp nifty_edison
cd086e5b4f5b nvidia/cuda:10.0-cudnn7-runtime-ubuntu16.04-my_1 "/bin/bash" 11 days ago Exited (130) 11 days ago wonderful_mestorf
113a51712f89 nvidia/cuda:10.0-cudnn7-runtime-ubuntu16.04-my "/bin/bash" 11 days ago Exited (0) 11 days ago tender_kirch
.................
11df2580f2b6 python3-cuda8 "/bin/bash" 8 months ago Exited (0) 8 months ago awesome_colden
6b0dcd3b4d8c python3-cuda8 "/bin/bash" 8 months ago Exited (0) 8 months ago festive_khayyam
92345b1425f1 python3-cuda8 "/bin/bash" 8 months ago Exited (0) 8 months ago sleepy_wescoff
(base) root@NLP:/home/hlx2/wieghts/test# docker ps -a|grep ubuntu
a2102e369d09 nvidia/cuda:10.0-cudnn7-runtime-ubuntu16.04-my_1 "/bin/bash" 11 days ago Up 11 days 0.0.0.0:9999->9999/tcp nifty_edison
cd086e5b4f5b nvidia/cuda:10.0-cudnn7-runtime-ubuntu16.04-my_1 "/bin/bash" 11 days ago Exited (130) 11 days ago wonderful_mestorf
..................
41daf352f1c6 ubuntu:15.10 "/bin/echo 'Hello wo…" 7 months ago Exited (0) 7 months ago awesome_curie
(base) root@NLP:/home/hlx2/wieghts/test# docker images|grep ubuntu
nvidia/cuda 10.0-cudnn7-runtime-ubuntu16.04-my_1 d83d5fbb8f13 11 days ago 1.57GB
nvidia/cuda 10.0-cudnn7-runtime-ubuntu16.04-my ecb1ddc85fb9 3 weeks ago 3.64GB
ubuntu latest f643c72bc252 4 weeks ago 72.9MB
nvidia/cuda 10.0-cudnn7-runtime-ubuntu16.04 a00b882cd4bc 2 months ago 1.33GB
ubuntu <none> 1d622ef86b13 8 months ago 73.9MB
ubuntu 14.04 6e4f1fe62ff1 12 months ago 197MB
ubuntu 15.10 9b9cb95443b5 4 years ago 137MB
(base) root@NLP:/home/hlx2/wieghts/test# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
305d47e07414 f643c72bc252 "/bin/bash" 23 minutes ago Up 23 minutes mystifying_mccarthy
.....................
6b0dcd3b4d8c python3-cuda8 "/bin/bash" 8 months ago Exited (0) 8 months ago festive_khayyam
92345b1425f1 python3-cuda8 "/bin/bash" 8 months ago Exited (0) 8 months ago sleepy_wescoff
(base) root@NLP:/home/hlx2/wieghts/test#
(base) root@NLP:/home/hlx2/wieghts/test# docker commit f643c72bc252 add_uncleyiba:1.0
Error response from daemon: No such container: f643c72bc252
(base) root@NLP:/home/hlx2/wieghts/test#
(base) root@NLP:/home/hlx2/wieghts/test# docker commit 305d47e07414 add_uncleyiba:1.0
sha256:b542d05022e18c625af846f9dc1b1f77060deb2c983268dd2645917ab5b27f0b
(base) root@NLP:/home/hlx2/wieghts/test# docker images|grep add
add_uncleyiba 1.0 b542d05022e1 About a minute ago 486MB
(base) root@NLP:/home/hlx2/wieghts/test# pwd
/home/hlx2/wieghts/test
(base) root@NLP:/home/hlx2/wieghts/test# ls
add.py data __pycache__ run.py
(base) root@NLP:/home/hlx2/wieghts/test# docker run -itd -p 8899:8778 -v/home/hlx2/wieghts/test/data:/add_uncleyiba/data add_uncleyiba:1.0 python /add_uncleyiba/run.py
bf13117e48e0b9ded4c28d566044dc8b7c1f377fdd36445d88261982d84452b1
(base) root@NLP:/home/hlx2/wieghts/test# curl -d 'a=5&b=7' 'localhost:8778/add'关掉之前在本机里运行的run.py
{"result": "OK", "id": "621125"}
(base) root@NLP:/home/hlx2/wieghts/test# curl -curl: (7) Failed to connect to localhost port 8778: 拒绝连接
(base) root@NLP:/home/hlx2/wieghts/test# curl -d 'a=5&b=7' 'localhost:8778/add'curl: (7) Failed to connect to localhost port 8778: 拒绝连接
(base) root@NLP:/home/hlx2/wieghts/test# docker run -itd -p 8899:8778 -v/home/hlx2/wieghts/test/data:/add_uncleyiba/data add_uncleyiba:1.0 python3 /add_uncleyiba/run.py
524a4640f9b9d9b560b7276e8494cf2421fd7665851675ea01d062f7e75a17f0
(base) root@NLP:/home/hlx2/wieghts/test# curl -d 'a=5&b=7' 'localhost:8778/add'curl: (7) Failed to connect to localhost port 8778: 拒绝连接
(base) root@NLP:/home/hlx2/wieghts/test# curl -d 'a=5&b=7' 'localhost:8899/add'{"result": "wrong"}(base)
root@NLP:/home/hlx2/wieghts/test# curl -d 'a=5&b=7' '{"result": "wrong"}
(base) root@NLP:/home/hlx2/wieghts/test# curl -d 'a=5&b=7' '{"result": "wrong"}
(base) root@NLP:/home/hlx2/wieghts/test# curl -d 'a=5&b=7' '{"result": "wrong"}
(base) root@NLP:/home/hlx2/wieghts/test# curl -d 'a=5&b=7' '{"result": "wrong"}
(base) root@NLP:/home/hlx2/wieghts/test# curl -d 'a=5&b=7' '{"result": "wrong"}
(base) root@NLP:/home/curl -d 'a=5&b=7' 'localhost:8899/add'{"result": "wrong"}
(base) root@NLP:/home/curl -d 'a=5&b=7' 'localhost:8899/add'
{"result": "wrong"}(base)root@NLP:/home/hlx2/wieghts/test# curl -d 'a=5&b=7' 'localhost:8899/add'
{"result": "wrong"}(base)root@NLP:/home/hlx2/wieghts/test#
(base) root@NLP:/home/hlx2/wieghts/test# docker run -it f643c72bc252 /bin/bash
b542d05022e1
(base) root@NLP:/home/hlx2/wieghts/test# docker run -it b542d05022e1 /bin/bash
进入add_uncleyiba
更多推荐
所有评论(0)