随笔分类 - python
python
django ORM操作
摘要:from django.contrib.auth.models import User from django.db.models import Q from django.db.models.functions import Lower from app.models import * 添加操作a
阅读全文
Django之Model操作数据库
摘要:ORM简介 O(objects):类和对象。R(Relation):关系,关系数据库中的表格。M(Mapping):映射。 Django ORM框架的功能: 建立模型类和表之间的对应关系,允许我们通过面向对象的方式来操作数据库。 根据设计的模型类生成数据库中的表格。 通过方便的配置就可以进行数据库的
阅读全文
django 模板
摘要:模板继承 模板继承和类的继承含义是一样的,主要是为了提高代码重用,减轻开发人员的工作量 {% extends 'base.html' %} base.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>
阅读全文
django响应html
摘要:from django.template import Template,Context,loader def index(context): temp = loader.get_template("table.html") return temp.render(context) def gette
阅读全文
Django model对象转字典
摘要:from django.forms.models import model_to_dict Group.objects.get(id=1).__dict__ model_to_dict(Group.objects.get(id=1)) User.objects.get(id=2).to_dict()
阅读全文
django自定义过滤器
摘要:https://docs.djangoproject.com/zh-hans/3.1/howto/custom-template-tags/ 代码布局 自定义的 tags 和 filters 会保存在模块名为 templatetags 的目录内。模块文件的名字即稍候你用来加载 tags 的名字,所以
阅读全文
python 抓包
摘要:Scapy方式 Scapy是一个强大的Python库,可用于创建、发送和解码网络数据包。它能够抓取和处理各种网络层协议、载荷和报头信息。 Scapy是一个全能的库,包含各种工具和功能。该库具有灵活性、可扩展性和实用性 from scapy.all import * pkts = sniff(ifac
阅读全文
python 日志
摘要:#coding:utf-8 import logging import sys import os import datetime from logging import handlers class Logger(object): def __init__(self,name): LOGGING_
阅读全文
requests 下载大文件
摘要:# -*- coding: utf-8 -*- from contextlib import closing from requests import get url = 'https://www.test.video/aa' # 但是使用with语句的时候是需要条件的,任何对象,只要正确实现了上下
阅读全文
python 协程
摘要:什么是协程 协程(co-routine,又称微线程、纤程)是一种多方协同的工作方式。协程不是进程或线程,其执行过程类似于 Python 函数调用,Python 的 asyncio 模块实现的异步IO编程框架中,协程是对使用 async 关键字定义的异步函数的调用。当前执行者在某个时刻主动让出(yie
阅读全文
python 读取yaml配置文件
摘要:# coding:utf-8 import yaml #pip install pyyaml import os import json base_path = os.path.dirname(os.path.abspath(__file__)) conf_file= os.path.join(ba
阅读全文
python configparser读取配置文件
摘要:# coding:utf-8 import configparser import os import json base_path = os.path.dirname(os.path.abspath(__file__)) conf_file= os.path.join(base_path,"con
阅读全文
python -c
摘要:#输出公网ippython3 -c "import requests;print(requests.get('http://jsonip.com/').json().get('ip'))" 20.23.45.23
阅读全文
python 保存数据为.csv文件
摘要:1、导包import csv2、创建或打开文件,设置文件形式csvfile = open('文件名.csv',mode='w',newline='')3、设置列名headers = ['列名1','列名2','列名3',...]4、创建DictWriter对象 write = csv.DictWri
阅读全文
python requests请求
摘要:#coding:utf-8 import requests import json try: from urllib.parse import urlencode except: from urllib import urlencode session = requests.Session() de
阅读全文
python操作git
摘要:安装模块 pip3 install gitpython #coding:utf-8 import os from git.repo import Repo from git.repo.fun import is_git_dir #pip3 install gitpython class GitRep
阅读全文
python requests-html
摘要:#pip install requests-html ''' 目标网站: https://pic.netbian.com ''' from requests_html import HTMLSession import re,os import requests from tqdm import t
阅读全文
Python解析url
摘要:# coding:utf-8 try: from urllib2 import urlparse except: from urllib import parse as urlparse d = urlparse.urlparse("http://www.test.com/a/b/c?name=李明
阅读全文
python requests
摘要:会话维持 import requests session = requests.Session() session.get('http://httpbin.org/cookies/set/num/123456') res = session.get('http://httpbin.org/cooki
阅读全文
StringIO 和 BytesIO
摘要:StringIO 要把 str 字符串写入内存中,我们需要创建一个 StringIO 对象,然后像文件一样对读取内容。其中 StringIO 中多了一个 getvalue() 方法,目的是用于获取写入后的 str。 # 定义一个 StringIO 对象,写入并读取其在内存中的内容 from io i
阅读全文
浙公网安备 33010602011771号