programming-language

参考资料

Python2 基础教程
https://www.runoob.com/python/python-tutorial.html

Python3 基础教程
https://www.liaoxuefeng.com/wiki/1016959663602400

shell中写Python代码并执行
https://blog.csdn.net/qq_35515661/article/details/93323380

python 快速启动http

python2
python -m SimpleHTTPServer 8000

python3
python3 -m http.server 8000

打开浏览器访问8000端口即可

帮助
python3 -m http.server -h

参考下载命令
wget http://192.168.1.20:8000/file.txt

java maven 学习

https://www.cnblogs.com/linux-wangkun/p/5670546.html
Maven项目打包成可执行Jar文件
https://www.cnblogs.com/linjiqin/p/10091113.html
java -cp 和 java -jar 的区别
https://www.jianshu.com/p/eabfb61e072a
http://maven.apache.org/plugins/maven-assembly-plugin/
mvn package
src/main/java/com/mycompany/app/App.java
java -cp /root/my-app/target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App

python 清理 nexus3 镜像

cat get_dp_images_nexus3.py
import os
import requests
import json

#os.system('> dpimages.txt')
dp_image_file="dpimages.txt"
docker_repository="docker-all"
url = "http://nexus3.as4k.com/service/rest/v1/search"

#docker_image_name="dp-customer-dashboard"
#docker_image_name="dp-thrall-webservice"
#docker_image_name="dp-thrall-mysql"
#docker_image_name="connect_sink_dp"
#docker_image_name="connect_source_dp"
docker_image_name="manager_connector"


querystring = {"repository":docker_repository,"docker.imageName":docker_image_name}

#querystring = {"repository":docker_repository} 
#querystring = {"repository":"docker-all","docker.imageName":"connect_sink_dp","docker.imageTag":"gws"} 

payload = ""
headers = {
    'Accept': "application/json",
}


def get_all_nexus3_images(url=url, querystring=querystring, payload=payload, headers=headers):
    response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
    dict_str = json.loads(response.text)
    for item in dict_str["items"]:
        image_list="%-40s %-50s %s" % (item["name"], item["version"], item["id"])
        print image_list
        with open(dp_image_file, 'a') as fb:
            fb.write(image_list)
            #fb.write("%-40s %-50s %s\n" % (item["name"], item["version"], item["id"]))
    #print dict_str["continuationToken"]
    if dict_str["continuationToken"]:
        querystring = {"repository":docker_repository,"docker.imageName":docker_image_name,"continuationToken":dict_str["continuationToken"]}
        #querystring = {"repository":docker_repository,"continuationToken":dict_str["continuationToken"]}
        get_all_nexus3_images(querystring=querystring)

get_all_nexus3_images()


#########################################################################################################################################################

cat delete_dp_image.sh
for i in $(cat real_delete_image_20190926.txt | awk '{print $NF}'); do
    curl -s -u admin:admin123 -X DELETE "http://nexus3.as4k.com/service/rest/v1/components/$i"
done

python2 和 python3 邮件发送

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
python xmail.py "all-sink-cnt-names.txt"    "xx全部souce connector名称"       "xys721@163.com"
python3 xmail.py "all-sink-cnt-names.txt"    "xx全部souce connector名称"       "xys721@163.com"
python xmail.py 邮件内容保存的文件名          邮件标题                          收件人
"""

import sys
import os
import json
# from datetime import datetime, timedelta
import datetime

from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib

# print sys.argv[1]

# ON open mail send otherwise OFF
MAIL_SWITCH = "ON"


def xsend_mail(subject, mail_msg, rec="xingyongsheng@as4k.com"):
    if MAIL_SWITCH == "OFF":
        return

    def _format_addr(s):
        name, addr = parseaddr(s)
        return formataddr((Header(name, 'utf-8').encode(), addr))

    to_addr = rec

    from_addr = 'qstest@163.com'
    password = 'qstest2019'
    smtp_server = 'smtp.163.com'

    msg = MIMEText(mail_msg, 'plain', 'utf-8')
    msg['From'] = _format_addr('<%s>' % from_addr)
    msg['To'] = _format_addr('<%s>' % to_addr)
    msg['Subject'] = Header(subject, 'utf-8').encode()

    server = smtplib.SMTP_SSL(smtp_server, 465)
    # server.set_debuglevel(1)
    server.login(from_addr, password)
    server.sendmail(from_addr, to_addr.split(','), msg.as_string())
    server.quit()


# xsend_mail(sub, "getResponse.status_code:" + getResponse.status_code, ops_email)

with open(sys.argv[1], 'r') as fb:
    xmail_content = fb.read()

xsend_mail(sys.argv[2], xmail_content, sys.argv[3])

run-a-single-python-script-with-docker

################################## 场景 ####################################################################
场景:客户机器环境复杂多变,而执行python脚本,完全不使用外部依赖包,将严重降低效率,在没有公网的情况下安装python依赖包,显然是一件费时费力的事情,如果还要涉及在系统中安装python2,或者从python2升级到python3,不可预料的风险将大大增加,针对上述问题,在客户机器里pull一个实现准备好的完整docker镜像,把镜像当成命令来用,将可使问题得到解决

################################## 定制自己需要的python镜像 ####################################################################
mkdir dp-python && cd dp-python

cat << 'EOF' > requirements.txt
certifi==2019.9.11
chardet==3.0.4
idna==2.8
redis==3.3.11
requests==2.22.0
urllib3==1.25.6
virtualenv==16.7.7
EOF

cat << 'EOF' > Dockerfile
FROM python:2.7.17

WORKDIR cd

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD [ "python", "./your-daemon-or-script.py" ]
EOF

docker build -t registry.as4k.com/dppython:2.7.17 .
docker push registry.as4k.com/dppython:2.7.17

################################## 把定制好的镜像当成命令用 ####################################################################
docker run -it --rm --name my-running-script  -v "$PWD":/usr/src/myapp -w /usr/src/myapp registry.as4k.com/dppython:2.7.17 python your-daemon-or-script.py

使用上述命令执行python脚本,所处的环境是容器里,因此访问其它容器服务需要做对应的调整,比如单机docker-compose环境下,可以增加--network参数
docker pull registry.as4k.com/dppython:2.7.17

docker run -it --rm --name my-running-script --network singlehost_default -v "$PWD":/usr/src/myapp -w /usr/src/myapp registry.as4k.com/dppython:2.7.17 python your-daemon-or-script.py


################################## 辅助资料 ####################################################################
https://hub.docker.com/_/python?tab=description&page=1&name=2.7.17

pip freeze > requirements.txt

docker run -d --name my-running-app --network singlehost_default -v "$PWD":/usr/src/myapp python:2.7.17 tail -F /tmp/tmp.txt

docker pull registry.as4k.com/dppython:2.7.17
docker image save -o dppython_2.7.17 registry.as4k.com/dppython:2.7.17

# md5sum  dppython_2.7.17.tar.gz 
d7952a6e71cf9f3f30e6fff9232da7eb  dppython_2.7.17.tar.gz

http://fs.as4k.com:7777/fs/mnt/fs/dppython_2.7.17.tar.gz

docker service create --replicas 1 --network datasystem --with-registry-auth --detach \
  --mount type=bind,src=/root/as4k,dst=/tmp  --name dppython registry.as4k.com/dppython:2.7.17  tail -F /tmp/tmp.txt