各类总结

总结

flask 启动自定义命令

python -m flask 脚本名

nohup 后台运行

(1)舍弃标准输出,将错误输出到log文件中
nohup ./program >/dev/null 2>log &

(2)如果错误信息也不想要的话:
nohup ./program >/dev/null 2>&1 &
案例:nohup python3 s5.py >/dev/null 2> error.out &

1. nohup command > myout.file 2>&1 &   
在上面的例子中,0 – stdin (standard input),1 – stdout (standard output),2 – stderr (standard error) ;
2>&1是将标准错误(2)重定向到标准输出(&1),标准输出(&1)再被重定向输入到myout.file文件中。
2. 0 22 * * * /usr/bin/python /home/pu/download_pdf/download_dfcf_pdf_to_oss.py > /home/pu/download_pdf/download_dfcf_pdf_to_oss.log 2>&1

这是放在crontab中的定时任务,晚上22点时候怕这个任务,启动这个python的脚本,并把日志写在download_dfcf_pdf_to_oss.log文件中

crontab执行定时任务

crontab -e   # 执行完添加需要执行的定时任务

minute       hour         day       month      week   需要执行的命令
分钟(0-50)   小时(0-23)    天(1-31)    月(1-12)   周(0-7)

时间位置如果不写用 * 表示,表示每分钟,每小时,每天..... 如果写了就会按照指定的时间执行
* * * * * /usr/bin/python /root/test/t1.py    # 表示每分钟都执行
1 * * * * /usr/bin/python /root/test/t1.py    # 表示每小时的第一分钟执行

# 在写命令的时候不要加上指定用户,只需要写执行的命令就行了

Git

# 删除本地分支
git branch -D 分支名

# 删除远程分支
git push origin --delete 分支名

# 刷新分支
git remote update origin --prune

# 保存此时状态
git stash

# git 删除 idea 文件加
git rm -r --cached .idea    删除 .idea 文件加
git commit -m '' 提交
git push origin devlop  更新线上代码

linux

linux端口操作

1)查找被占用的端口:
netstat -tln | grep 8000
tcp        0      0 192.168.2.106:8000      0.0.0.0:*               LISTEN  
        
2)查看被占用端口的PID:
sudo lsof -i:8000
 
COMMAND PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   850     root    6u  IPv4  15078      0t0  TCP 192.168.2.106:8000 (LISTEN)
nginx   851 www-data    6u  IPv4  15078      0t0  TCP 192.168.2.106:8000 (LISTEN)
nginx   852 www-data    6u  IPv4  15078      0t0  TCP 192.168.2.106:8000 (LISTEN)
    
3)kill掉该进程
sudo kill -9 850

显示当前服务器上所有端口及进程服务,与grep结合可查看某个具体端口及服务情况
netstat -ntlp   //查看当前所有tcp端口·
netstat -ntulp |grep 80   //查看所有80端口使用情况·
netstat -an | grep 3306   //查看所有3306端口使用情况·

基础命令操作

# 安装 ifconfig
sudo yum install net-tools

# 安装 ssh
yum install -y openssl openssh-server

# 安装 ip addr
yum install iproute2 iproute2-doc

安装python

1.切换到root用户
    su
2 下载tar包 
	wget http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tgz
3  安装
	tar -zxvf Python-2.7.5.tgz
	cd Python-2.7.5
	./configure --prefix=/usr/local/python27
	make&&make install
    
# 如果编译的时候遇到报错:make: *** No targets specified and no makefile found.  Stop.
wget http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.6.tar.gz

tar zxvf ncurses-5.6.tar.gz

cd ncurses-5.6

./configure -prefix=/usr/local -with-shared -without-debug

make

make install

PS:执行之前要确保gcc是已经安装的,直接yum就可以的。还要确保最新版系统软件用update来搞定!

删除centos7中自带有python2.7

(1)强制删除已安装python及其关联
rpm -qa | grep python | xargs rpm -ev --allmatches --nodeps

(2)删除残余文件
whereis python | xargs rm -frv

Linux pip命令报错 -bash: pip: command not found

下载
$ wget https:``//bootstrap``.pypa.io``/get-pip``.py

安装
$ python get-pip.py

查看版本
$ pip -V

查看安装路径:
find` `/ -name pip

软连接
ln` `-sv ``/usr/local/python/bin/pip` `/usr/bin/pip

安装crontab

#安装crontab
yum install cronie
 
#启动服务
service crond start

linux 禁止某个 ip 访问

要封停一个IP,使用下面这条命令:
iptables -I INPUT -s ... -j DROP

要解封一个IP,使用下面这条命令:
iptables -D INPUT -s ... -j DROP

参数-I是表示Insert(添加),-D表示Delete(删除)。后面跟的是规则,INPUT表示入站,...表示要封停的IP,DROP表示放弃连接。

此外,还可以使用下面的命令来查看当前的IP规则表:
iptables --list

比如现在要将123.44.55.66这个IP封杀,就输入:
iptables -I INPUT -s 123.44.55.66 -j DROP

要解封则将-I换成-D即可,前提是iptables已经有这条记录。如果要想清空封掉的IP地址,可以输入:
iptables -flush

要添加IP段到封停列表中,使用下面的命令:
iptables -I INPUT -s 121.0.0.0/8 -j DROP

后台运行 python 脚本

nohup python text.py &

shell脚本

#!/bin/bash
lsof -i:8002|awk '{print $2}'|xargs kill -9
nohup gunicorn --workers=3 manage:app -b 0.0.0.0:8002  > ./nohup.out 2>&1 &

# ps aux|grep "rox_master.py"|grep -v PID|awk '{print $2}'|xargs kill -9
cd schedule
nohup python rox_master.py > nohup.out 2>&1 &
cd ..

# ps aux|grep "rox_worker.py"|grep -v PID|awk '{print $2}'|xargs kill -9
for((i=12350; i<12352; i++))
do
cd worker_$i && nohup python3 rox_worker.py $i >/dev/null 2>&1 &
done


--------------------------------------------------------------------
shell脚本
1. 开头必须  #!/bin/bash
2. nohup   后台运行
3. > ./nohup.out 2>&1 &   将错误信息,标准输出写入到nohup.out文件中

基础项目启动

# vue项目
yarn install
yarn run serve

flask项目
pipenv run flask run --port=5080

flask中写视图函数
直接在vue中写视图函数,路由会自动调用

MySQL

# 更改数据库字段类型
alter table 表名 modify column 字段名 新字段类型;
alter table exec_results modify column body longtext;

# 新增字段
alter table exec_history add mark int(11);

查看用户权限  show grants for wang;
赋予用户权限  GRANT ALL PRIVILEGES ON `libra1`.* TO 'wang'@'%'
重置密码:
  1.service mysqld stop    # 关闭服务
  2.mysqld --user=mysql --skip-grant-tables --skip-networking &
  3.mysql    # 不需要密码进入MySQL
  4.update mysql.user set authentication_string=password('123456') where user='root';
  # 更改数据库密码,mysql5.7以后没有password字段,更换成 authentication_string
  5.退出重新登陆

RPC

python调用rpc实现分布式系统
rpc 一般俗称,远程过程调用,把本地的函数,放到远端去调用。

通常我们调用一个方法,譬如: sumadd(10, 20),sumadd方法的具体实现要么是用户自己定义,要么存在于该语言的库函数中,也就说在sumadd方法的代码实现在本地,它是一个本地调用!

“远程调用”意思就是:被调用方法的具体实现不在程序运行本地,而是在别的某个地方(分布到各个服务器),但是用起来像是在本地。

rpc远程调用原理 :比如 A调用B提供的remoteAdd方法:首先A与B之间建立一个TCP连接;然后A把需要调用的方法名(这里是remoteAdd)以及方法参数(10, 20)序列化成字节流发送出去;B接受A发送过来的字节流,然后反序列化得到目标方法名,方法参数,接着执行相应的方法调用(可能是localAdd)并把结果30返回;A接受远程调用结果,然后do()。

RPC框架也就是把上线说的具体的细节封装起来,给用户好用的API使用(提示:有些远程调用选择比较底层的socket协议,有些远程调用选择比较上层的HTTP协议);

一般rpc配合http协议的多点,也就是走http的多。 当然还是看应用,我曾经一共的rpc框架是基于zeromq的zerorpc。速度是挺快,server和client都有python的gevent支持,速度没道理慢。(有兴趣的,可以看看有关zerorpc的文章 http://rfyiamcool.blog.51cto.com/1030776/1254000 )最少要比python本身的xml-rpc要快。 rpc over http(基于http的rpc)有两种协议,一种是xml-rpc ,还有一个是 json-rpc。

XML-RPC:XML Remote Procedure Call,即XML远程方法调用,利用http+xml封装进行RPC调用。基于http协议传输、XML作为信息编码格式。一个xml-rpc消息就是一个请求体为xml的http-post请求,服务端执行后也以xml格式编码返回。这个标准面前已经演变为下面的SOAP协议。可以理解SOAP是XML-RPC的高级版本。


# 服务端
import sys
from xmlrpc.server import SimpleXMLRPCServer
import socketserver

s = SimpleXMLRPCServer(('0.0.0.0', int(sys.argv[1])))
s.register_function(compute, "compute")    # 注册一个连接的时候调用的方法
s.serve_forever()    # 类似于后台监听

print(sys.argv)    # ['D:/dfc-project/zipline_pro/s4.py', '12345']


# 客户端
s = ServerProxy("http://{0}:{1}".format(*worker), allow_none=True)    # 连接服务端的rpc
_uuid = s.compute(exec_id, context, script, factor_name)    # 调用rpc注册的方法,返回执行结果


# 终止任务
s = ServerProxy("http://{0}:{1}".format(*worker), allow_none=True)
s.kill(body.get("exec_id"))

celery总结

# 公司电脑模块配置 celery 4.2.2    redis 3.2.0
# 启动celery 
celery worker -A tasks -l info -P eventlet

# 注册任务
result = add.delay(10, 20)   # 此时已经将任务放到队列中执行了

# 获取任务结果
asyn = AsyncResult(id=result.id, app=app)
asyn.get()

# 终止任务
asyn.revoke()

Anconda 创建py2环境

# 创建一个环境
conda create -n py27 python=2.7 
或者conda create --name py27 python=2.7

# win激活环境
activate py2
#linux激活
source activate py27

#win退出
deactivate
#linux退出

# 添加Anaconda的TUNA镜像
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

# TUNA的help中镜像地址加有引号,需要去掉

# 设置搜索时显示通道地址
conda config --set show_channel_urls yes

pipenv 创建虚拟环境

pipenv install    # 创建虚拟环境
pipenv shell      # 进入虚拟环境
pipenv --python D:Anaconda3envspy2python.exe    # 指定python路径

前端

# es6 列表去重
# 方法一 
if (shareMaps[n.eaid].indexOf(`${n.user}`) < 0) {
    shareMaps[n.eaid].push(`${n.user}`)
    }

# 方法二
shareMaps[n.eaid].push(`${n.user}`)
shareMaps[n.eaid] = Array.from(new Set(shareMaps[n.eaid]))

# 列表内的值相加
blanceArr.reduce((a, b) => a + b)

# Vue 中对渲染的数据进行处理
<template v-else>{{ cleanRow(scope.row.balance) }}</template>

cleanRow (scope) {
    if (parseInt(scope).toLocaleString() === 'NaN') {
        return ''
    } else {
        return parseInt(scope).toLocaleString()
    }
  },

# 对 Array 数据的处理,变成列表的形式
this.fundMap = res.reduce(function (map, obj) {
	map[obj.amac_id] = obj
    return map
}, {})

# 解构赋值
... 操作,可以把对象拆开
{...{}} or {...[]}

后端

# 生成式 生成 字典
{eaid: fund for eaid, fund in _accounts}

# 列表生成式添加判断
fund_names = [fund.fund_name if fund.amac_id in amac_id_list else None for _, fund in _accounts]
原文地址:https://www.cnblogs.com/whkzm/p/13783126.html