常用代码
re正则
((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3} # 匹配ip https://www.jianshu.com/p/82886d77440c
result = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", string_ip) # 简单的从长文本中提取中提取ip地址 https://www.cnblogs.com/brogong/p/7929298.html
########################################## A B C 内网ip ##################################################################
if re.findall(r'^10\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])$', ip): # A类网段的正则表达式
print(ip)
if re.findall(r'^172\.(1[6789]|2[0-9]|3[01])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])$', ip): # B类网段的正则表达式
print(ip)
if re.findall(r'^192\.168\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])$', ip): # B类网段的正则表达式
print(ip)
virtualenv
pip3 install virtualenv
第一步,创建目录:
mkdir myproject
cd myproject/
michael$
第二步,创建一个独立的Python运行环境,命名为venv:
$ virtualenv --no-site-packages venv # --no-site-packages 系统Python环境中的所有第三方包都不会复制过来
第三步,进入独立环境
source venv/bin/activate
创建虚拟环境的时候 “virtualenv --no-site-packages venv”,如果virtualenv版本大于20,加上--no-site-packages时候会报错,默认版本大于20是不用加的
进入虚拟环境的时候 ,作者使用的source命令是mac电脑独有的,windows的话使用cd 进入scripts目录就好了
requests模块
pip install -U requests[socks]
#! -*- coding:utf-8 -*-
import requests
from multiprocessing.pool import ThreadPool
# 取消验证警告
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
headers_str = """Host: api.fofa.so
User-Agent: Mozilla/5.0
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Referer: https://fofa.so/
Authorization: eyJhbGciOiJIUzUxMiIsImtpZCI6Ik5XWTV
Cache-Control: no-cache
Origin: https://fofa.so
Connection: close"""
headers = dict([line.split(": ",1) for line in headers_str.split("\n")])
proxies = {
"http": "socks5://127.0.0.1:8080",
}
ret = requests.get(url=url, headers=headers, proxies=proxies, verify=False)
pool.apply_async(one, (x["id"],))
pool.close()
pool.join()
list列表
aList = [1,2,3]
bList = ['www', 'pythontab.com']
aList.extend(bList)
print(aList)
输出为:
[1, 2, 3, 'www', 'pythontab.com']
dict字典
a = {"a":"a", "b":"b"}
b = {"1":"1", "2":"2"}
a.update(b) ==> {"a":"a", "b":"b", "1":"1", "2":"2"}
a = {"a":"a", "b":"b"}
b = {"1":"1", "a":"2"}
a.update(b) ==> {'a': '2', 'b': 'b', '1': '1'} # 如果key一样,后添加的会覆盖之前的数据
获取当前路径
import os
path = os.path.realpath(__file__) # C:\Users\Administrator\Desktop\tools\code\筛选日志中的ip\cheak_ip.py
path_2 = os.path.abspath(__file__) # C:\Users\Administrator\Desktop\tools\code\筛选日志中的ip\cheak_ip.py
path_3 = os.path.split(os.path.realpath(__file__)) # ('C:\\Users\\Administrator\\Desktop\\tools\\code\\筛选日志中的ip', 'cheak_ip.py')
print("####")
print(path_3)
print 和 sys.stdout.write 屏幕输出
while True:
# pool.terminate() # 进程退出
sys.stdout.write(' ' * 20)
sys.stdout.write("\r[*] " + str(pool.q.qsize()))
# sys.stdout.flush()
# print(" " * 20, end="\r")
# print("\r[*] ",str(pool.q.qsize()),end="",flush=True)
time.sleep(0.2)
if pool.q.qsize() <= 1:
# print(" " * 20, end="\r")
sys.stdout.write(' ' * 20 +'\r')
time.sleep(0.2)
break
#######################################################
print只支持python3
print(" " * 20, end="\r")
print("\r[*] ","this test",end="",flush=True)
python2 3 通用以下方法
sys.stdout.write(' ' * 20)
sys.stdout.write("\r[*] " + "thistest")
ctrl + c 退出实现
while True:
try:
pass
except KeyboardInterrupt: # 异常捕获 ctrl + c
sys.stdout.write("\rCTRL+C detected: Pausing threads, please wait...")
pool.terminate() # 调用线程池的退出方法, 进程退出
break
ip排序
with open("ip.txt", "r") as infile:
iplist = sorted([i.strip() for i in infile.readlines()], key = lambda x: int(''.join((lambda a:lambda v:a(a,v))(lambda s,x: x if len(x) == 3 else s(s, '0'+x))(i) for i in x.split('.'))))
# with open("output.txt", "w") as outfile:
# outfile.write("\n".join(i for i in iplist))
for ip in iplist:
print(ip)
log记录
import logging, os
def loging():
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(message)s'
)
if not os.path.exists('log'):
os.mkdir('log')
logger = logging.getLogger('LogInfo')
fh = logging.FileHandler('log/process.log')
fh.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
return logger
logger = loging() # 日志模块
# logger.info('read risk result')
# print(logger, type(logger))
literal_eval
import ast
user = '{"name" : "john", "gender" : "male", "age": 28}'
user_dict = ast.literal_eval(user)
print(user_dict)
print(type(user_dict))
打印的结果:
{'gender': 'male', 'age': 28, 'name': 'john'} <type 'dict'>
标签:25,常用,ip,代码,print,path,os,pool
From: https://www.cnblogs.com/startstart/p/16908833.html