Python¶
# python2 SimpleHTTPServer
python -m SimpleHTTPServer 8008
# python3
python -m http.server 8008
# 查找python安装包的路径 site-packages
python -m site # 系统目录
python -m site --user-site # 用户目录
打包¶
- python3编译成pyc文件
python3 -m compileall -b . # -b: 生成的pyc与源代码在同一目录
find . -name '*.py' |xargs rm -rf # . 删除py文件
find . -name 'pycache' |xargs rm -rf # 删除pycache目录
国内镜像源¶
- Linux
mkdir $HOME/.pip
tee $HOME/.pip/pip.conf <<-'EOF'
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF
- Windows
在文件夹的地址栏输入 %appdata% (即进入这个文件夹)。
在当前文件夹下新建一个pip文件夹。
进入pip文件夹,新建一个pip.ini文件
virtualenv¶
# old version
#virtualenv --no-site-packages venv
#virtualenv -p /usr/bin/python3.6 --no-site-packages venv
# new version 20.0.13
virtualenv --python=python3 venv
source venv/bin/activate #启动环境
deactivate # 退出删除环境
pip freeze # 查看当前安装版本
pip freeze > requirements.txt
python库¶
# 键盘事件监听
from pykeyboard import PyKeyboardEvent
import time
class TapRecord(PyKeyboardEvent):
def __init__(self):
PyKeyboardEvent.__init__(self)
def tap(self, keycode, character, press):
print(time.time(), keycode, character, press)
t = TapRecord()
t.run()
# 查看字符串 编码
import chardet
fencoding=chardet.detect(u"test string")
print fencoding
# 从URL地址提取文件名
import os
url = 'http://www.jb51.net/images/logo.gif'
filename = os.path.basename(url)
print(filename)
# 去除扩展名
print os.path.splitext(filename)[0]
# 打印方法名
import inspect
import sys
for method in inspect.getmembers(sys.path):
print method
Linux 下编译windows Exe¶
sudo apt-get install wine32 winetricks
winetricks python
cd ~/.wine/drive_c/Python26
#wine msiexec /i python-2.7.15.msi /L*v log.txt
wine msiexec /i python-2.7.15.msi
wine python.exe Scripts/pip.exe install pyinstaller -i https://pypi.mirrors.ustc.edu.cn/simple
wine python.exe Scripts/pip.exe install --upgrade pip -i https://pypi.mirrors.ustc.edu.cn/simple
#wine python.exe Scripts/pip.exe install pyserial -i https://pypi.mirrors.ustc.edu.cn/simple
cd ~/workspace
tee HelloWorld.py <<-"EOF"
print('hello world!')
EOF
wine ~/.wine/drive_c/Python27/Scripts/pyinstaller.exe --onefile HelloWorld.py
wine dist/HelloWorld.exe
sudo dpkg --add-architecture i386 # 开启32位架构支持
sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/' /etc/apt/sources.list
apt-get update
apt-get install -y curl
apt-get install -y wine32
#apt-get install -y --fix-missing winetricks
curl -o python-2.7.15.msi https://www.python.org/ftp/python/2.7.15/python-2.7.15.msi
wine msiexec /i python-2.7.15.msi /q #Dockerfile 有问题???, 手动安装没问题
rm -f python-2.7.15.msi
PY_HOME=${HOME}/.wine/drive_c/Python27
wine ${PY_HOME}/python.exe ${PY_HOME}/Scripts/pip.exe install pyinstaller -i https://pypi.mirrors.ustc.edu.cn/simple
wine ${PY_HOME}/python.exe ${PY_HOME}/Scripts/pip.exe install --upgrade pip -i https://pypi.mirrors.ustc.edu.cn/simple
build int docker¶
build install python¶
# 编译前安装 , 否则pip 报错误
sudo apt-get install openssl
sudo apt-get install libssl-dev
示例¶
UDP 接收超时¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #coding=utf-8
import socket,sys
import time
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
IP=socket.gethostbyname(socket.gethostname())
s.settimeout(30) #设置超时
s.bind((IP,1080))
print ("等待数据中。。。。")
while True:
try:
s.settimeout(0.02) # 200ms 超时
d,a=s.recvfrom(8192)
print ("%s 在 %s发来数据 :%s "%(a,time.ctime(),d))
# s.sendto('[%s] %s'%(time.ctime(),d),a)
# print ("收到数据并且返回到:",a)
except socket.timeout:
print ("时间到!")
s.close()
|