练习python+bs4提取likebookmark网址导航的所有链接

from bs4 import BeautifulSoup
import re
import sqlite3
conn = sqlite3.connect('onenav.db3')
print("数据库打开成功")
c = conn.cursor()
c.execute('''delete from on_links;''')
c.execute('''update sqlite_sequence SET seq = 0 where name ='on_links';''')
c.execute('''delete from on_categorys;''')
c.execute('''update sqlite_sequence SET seq = 0 where name ='on_categorys';''')
conn.commit()
print("数据清空完毕")
f = open('555.txt', encoding='utf-8')
html = f.read()
f.close()
soup = BeautifulSoup(html, 'html.parser')
div_tags = soup.find_all('div', {'class': 'site-main-li'})
category_id = 0
for div_tag in div_tags[1:-1]:
    category_id += 1
    category = div_tag.find('div', {'class', 'site-tit'}).get_text().strip()
    links = div_tag.find_all('div', {'class', 'list siteList'})
    print(category)
    c.execute('''insert into on_categorys (`id`, `name`) values (?, ?);''', (category_id, category))
    for link in links:
        link_id = link.get('data-id')
        if link_id:
            url = link['data-links']
            title = re.sub(r'<.*>', '', link.find('p', class_="title").get_text().strip())
            print('\t'.join([url, title]))
            c.execute('''insert into on_links (`fid`, `title`, `url`) values (?, ?, ?);''', (category_id, title, url))
    print('---')
conn.commit()
conn.close()

python进行DNS解析的包package好用的resolver

import dns.resolver
query_object = dns.resolver.resolve('txt-www.nohup.net', 'TXT')
print('*' * 100)
# 查看response对象
response_object = query_object.response
print('应答对象:{}'.format(response_object))
print('*' * 100)
# 查询answer对象
answer_object = response_object.answer
print('解析对象:{}'.format(answer_object))
print('*' * 100)
# 查看解析条目对象
answer_object = response_object.answer
for query_item in answer_object:
    print('查询条目:{}'.format(query_item))
print('*' * 100)
# 解析后的记录条目
query_item = response_object.answer[0]
# print(query_item)
for item in query_item:
    print('解析记录:{}'.format(item))
print('*' * 100)

宝塔自定义插件Demo增加IP地理位置为区县级和街道级

参考代码:

demo_main.py 里的get_logs函数里,增加:

        for log_item in log_list:
            log_str = log_item.get("log")
            left_ip = log_str.split('(')[0]
            right_op = log_str.split(')')[1]
            r = urlopen("https://api.ipplus360.com/ip/geo/v1/district/?key=YOUR_KEY&ip="+left_ip )
            json_str = r.read()
            r.close()
            middle = json_str
            response = json.loads(json_str)
            middle = response.get("data").get("prov") + response.get("data").get("city") + response.get("data").get("district")
            log_item["log"] = left_ip + middle + right_op

python之socketserver模块

服务端:

socketserver
time
MyTCPHandler(socketserver.BaseRequestHandler):
    handle():
        .data = .request.recv().strip()
        (.format(.client_address[]))
        :
            (.data)
            .request.send(.data.upper())
            time.sleep()
__name__ == :
    HOSTPORT = socketserver.TCPServer((HOSTPORT)MyTCPHandler) server:
        server.serve_forever()

python listen参数值是什么意思

server:

import socket 
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)             # 创建socket对象
s.bind(('0.0.0.0', 8000))                                      # 绑定地址
s.listen(2)                                                  # 建立5个监听
while True:
    conn,addr= s.accept()                                       # 等待客户端连接
    print('欢迎{}'.format(addr))                              #打印访问的用户信息
    while True:
        data=conn.recv(1024) 
        dt=data.decode('utf-8')                                 #接收一个1024字节的数据 
        print('收到:',dt)
        aa="456"
        if aa=='quit':
            conn.close()                                        #关闭来自客户端的连接
            s.close()                                           #关闭服务器端连接
        else:
            conn.send(aa.encode('utf-8'))