1. 拼接路径时,python 用
os.path.join("a","b","c","d")
会根据操作系统的不同拼接出正确的路径。
2. 多行字符串格式化。
例子:
raws="""
The first string is %s
The second string is %s
The last string is %s
"""%('1','2','3')
记得格式化"""%('1','2','3')
这里不要有空格
3. requests获取网页响应时间用法:单位是微秒
import requests
r = requests.get("http://www.baidu.com")
r.elapsed.microseconds
4. pymongo 显示报错信息cursor id is not valid at server
db.collection.find() 返回的是一个cursor,而不是所有数据,
比较好的解决办法是:控制batch_size的数量,让其在10分能跑完,避免cursor超时。
for doc in db.collection.find().batch_size(50):
5. python 快速生成包含同一元素的列表方法:
port = 22
portList = [port] * 10
#生成结果[22,22,22,22,22,22,22,22,22,22]
6. requests的使用
#忽略证书错误
requests.get('https://baidu.com', verify=False)
#使用代理获取百度首页
import requests
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
}
## 定义代理的字典
proxies = {
# "http":"http://114.18.52.4:6666"
# "https":"https://114.19.30.126:8888"
# "http":"http://username:password@ip:端口号" #带账号密码的代理
#"https": "https://username:password@ip:端口号"
"http": "http://120.28.18.93:8001"
}
## 使用代理给服务器发送请求
response = requests.get("https://www.baidu.com", proxies=proxies, headers=headers)
## 获取状态
print(response.status_code)
print(response.content.decode())
7. pymongo 把结果json化
#数据库连接文件dbc.py
import pymongo, sys, traceback
MONGODB_CONFIG = {
'host': '192.168.1.1',
'port': 27017,
'db_name': 'test',
'username': 'test',
'password': 'test'
}
class MongoConn(object):
def __init__(self):
try:
self.conn = pymongo.MongoClient(MONGODB_CONFIG['host'], MONGODB_CONFIG['port'])
self.db = self.conn[MONGODB_CONFIG['db_name']]
self.username = MONGODB_CONFIG['username']
self.password = MONGODB_CONFIG['password']
if self.username and self.password:
self.connect = self.db.authenticate(self.username, self.password)
else:
self.connect = True
except Exception:
print(traceback.format_exc())
print("Connect db Fail!!")
sys.exit(1)
#结果按json格式输出
from dbc import MongoConn
from bson.objectid import ObjectId
from bson import json_util as jsonb
md = MongoConn()
result = md.db['sshDb'].find({"tryPassWord": {"$nin": [password]}}, {'ip': 1, "_id": 0}).batch_size(10)
res = json.loads(jsonb.dumps(result))
print(res)
8. 打开大文件如何减少内存占用,错误提示MemoryError。
#文本文件用直接迭代文件
with open('password.txt', 'r', 'utf-8', buffering=True)as lines:
for line in lines:
print(line)
#或者
with open('password.txt', 'rb', 'utf-8', buffering=True) as f:
while True:
line = f.readline()
if line:
print(line)
else:
break
#二进制文件
with open('password.txt', 'rb') as f:
while True:
buf = f.read(1024)
if buf:
sha1Obj.update(buf)
else:
break
小端转大端(大端为适合人类阅读顺序)
#适用于python2
"6fe28c".decode('hex')[::-1].encode('hex_codec')
# output: '8ce26f'
jupyter notebook打开命令
#cmd 下运行
jupyter notebook
conda常用命令
#创建环境,py27是环境别名,2.7是python的版本
conda create -n py27 python=2.7
#查看所有安装的环境
conda env list
#激活环境
conda activate py27
在当前目录安装lxml依赖包
pip install lxml -t .
网友评论