Knight000's Blog

随缘写写,不定时更新

0%

一些常用指令和技巧之类的

记录一些常用的东东啥的

指令

Git

PUSH

1
2
3
git add .
git commit -m "v1.0.1"
git push

PULL

1
2
3
git fetch --all
git reset --hard origin/master
git pull origin master

FAST API

RUN

1
uvicorn main:app --reload --port 80 --host 0.0.0.0

跨域

1
2
3
4
5
6
7
8
9
10
11
12
app = FastAPI()

origins = [
"*"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

POST以data接收

1
2
3
4
5
6
from pydantic import BaseModel
class Item(BaseModel):
apikey: str
text: str
@app.post("/")
def _(item:Item):

要先定义一个BaseModel然后在定义函数的时候使用他

Windows Shell

查询指定端口占用

1
2
netstat -aon|findstr "PORT"
tasklist|findstr "PID"

其实写了个python脚本来整这个,不过也一样

Linux

CentOS

强制重置虚拟机网卡
1
2
3
systemctl stop NetworkManager
systemctl disable NetworkManager
systemctl restart network

编程

Python

File

当前工作目录
1
2
import os
path = os.path.abspath(os.curdir)

NoneBot2

快速开始
1
2
3
4
5
6
7
from nonebot import on_command
from nonebot.adapters.cqhttp import MessageSegment, Bot, Event

command_name = on_command("指令名")
@command_name.handle()
async def func_name(bot: Bot, event: Event, state: dict):
await command_name.finish("message")
base64编码图片并发送

网页

1
2
3
4
5
6
import base64
import requests
web_image = requests.get(web['data'][0]['url'], timeout=20)
if web_image.status_code == 200:
images = f"base64://{base64.b64encode(web_image.content).decode()}"
await pic.finish(MessageSegment.image(images))

本地

1
2
3
4
5
6
7
import base64
with open(r"src\images\help.png", "rb") as f:
image = f"base64://{base64.b64encode(f.read()).decode()}"
help = on_command("help")
@help.handle()
async def help_func(bot: Bot, event: Event, state: dict):
await help.finish(MessageSegment.image(image))

Jupyter

/root/.jupyter/jupyter_notebook_config.py

生成配置文件
1
jupyter notebook --generate-config
临时启动
1
jupyter notebook --ip=0.0.0.0 --no-browser --allow-root --notebook-dir=/home/python/jupyter-notebook
配置文件相关
1
2
3
4
5
6
## 允许notebook在root用户下运行.
c.NotebookApp.allow_root = True
## notebook服务会监听的IP地址.
c.NotebookApp.ip = '0.0.0.0'
## 用于笔记本和内核的目录。
c.NotebookApp.notebook_dir = '/home/jupyter'

datetime

datetime对象可以相互加减,然后通过res.days这样的方式来输出日期之类的。

字符串格式化为datetime对象
1
datetime.datetime.strptime("11月25日 01:26","%m月%d日 %H:%M")
替换参数
1
2
# 用当前年份替换数据的年份
datetime.replace(year=datetime.datetime.now().year)