1,any用法
lst = [1,2,3,4,5] res = [12,23,34,45] print(any(i in res for i in lst)) #res中没有和lst相同的数据,所以是false
==>false
2,类里面的单例模式
import threading class Ioloop(object): _instance_lock = threading.Lock() @staticmethod def instance(): if not hasattr(Ioloop,'_instance'): with _instance_lock: if not hasattr(Ioloop,'_instance'): Ioloop._instance=Ioloop() return Ioloop._instance
3,字典添加内容
可用update
dct = {'name':'forjie','age':'18'}
dct.update({'sex':'male'})
4,获取当前用户是从哪跳转到当前页面request.META
里面包含了各种信息,路径等等
获取跳转之前页面: request.META.get('HTTP_USER_AGENT')
{'PATH': '/home/panyu/data_center/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/sbin:/usr/sbin',
'XAUTHORITY': '/home/panyu/.Xauthority', 'XMODIFIERS': '@im=fcitx',
'XDG_DATA_DIRS': '/home/panyu/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share',
'GDMSESSION': 'deepin', 'QT_IM_MODULE': 'fcitx', 'GTK_IM_MODULE': 'fcitx', 'XFILESEARCHPATH': '/usr/dt/app-defaults/%L/Dt',
'LANG': 'zh_CN.UTF-8', 'DBUS_SESSION_BUS_ADDRESS': 'unix:path=/run/user/1000/bus',
'XDG_SEAT_PATH': '/org/freedesktop/DisplayManager/Seat0', 'XDG_SESSION_ID': '2',
'XDG_SESSION_TYPE': 'x11', 'PS1': '(data_center) ', 'XDG_CURRENT_DESKTOP': 'Deepin',
'DISPLAY': ':0', 'LD_LIBRARY_PATH': '/usr/lib/pycharm-professional/bin:',
'QT4_IM_MODULE': 'fcitx', 'LOGNAME': 'panyu', 'PWD': '/home/panyu/Downloads/project/data_center',
'PYCHARM_HOSTED': '1', 'LANGUAGE': 'zh_CN', 'PYTHONPATH': '/home/panyu/Downloads/project/data_center',
'SHELL': '/bin/zsh', 'QT_SCALE_FACTOR': '', 'PYTHONIOENCODING': 'UTF-8', 'XDG_GREETER_DATA_DIR': '/var/lib/lightdm/data/panyu',
'GIO_LAUNCHED_DESKTOP_FILE': '/usr/share/applications/jetbrains-pycharm.desktop', 'GPG_AGENT_INFO': '/run/user/1000/gnupg/S.gpg-agent:0:1',
'DESKTOP_SESSION': 'deepin', 'USER': 'panyu', 'GIO_LAUNCHED_DESKTOP_FILE_PID': '6630',
'GNOME_DESKTOP_SESSION_ID': 'this-is-deprecated', 'SSH_AUTH_SOCK': '/run/user/1000/keyring/ssh',
'XDG_SEAT': 'seat0', 'DJANGO_SETTINGS_MODULE': 'data_center.settings', 'VIRTUAL_ENV': '/home/panyu/data_center',
'CLUTTER_IM_MODULE': 'xim', 'PYTHONUNBUFFERED': '1', 'XDG_SESSION_PATH': '/org/freedesktop/DisplayManager/Session0',
'NLSPATH': '/usr/dt/lib/nls/msg/%L/%N.cat', '_JAVA_OPTIONS': ' -Dawt.useSystemAAFontSettings=gasp', 'XDG_VTNR': '1',
'XDG_SESSION_DESKTOP': 'deepin', 'XDG_RUNTIME_DIR': '/run/user/1000', 'HOME': '/home/panyu', 'SHLVL': '0',
'CELERY_LOADER': 'djcelery.loaders.DjangoLoader', 'TZ': 'Asia/Shanghai', 'RUN_MAIN': 'true',
'SERVER_NAME': 'localhost', 'GATEWAY_INTERFACE': 'CGI/1.1', 'SERVER_PORT': '8000', 'REMOTE_HOST': '',
'CONTENT_LENGTH': '', 'SCRIPT_NAME': '', 'SERVER_PROTOCOL': 'HTTP/1.1', 'SERVER_SOFTWARE': 'WSGIServer/0.2',
'REQUEST_METHOD': 'GET', 'PATH_INFO': '/v1/capital_province/', 'QUERY_STRING': '',
'REMOTE_ADDR': '127.0.0.1', 'CONTENT_TYPE': 'text/plain',
'HTTP_AUTHORIZATION': 'JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo2LCJlbWFpbCI6InBhbnl1QHlpbWVpamlhbi5jbiIsInVzZXJuYW1lIjoicGFueXUiLCJleHAiOjE1NDQwMTkyMzl9.FFudYPH9kfeA2qgZucKyKZXSRmKz7mvfRh0D735LST4',
'HTTP_CACHE_CONTROL': 'no-cache', 'HTTP_POSTMAN_TOKEN': '1dcd0114-46c0-4fca-8039-93046669758d',
'HTTP_USER_AGENT': 'PostmanRuntime/7.1.1', 'HTTP_ACCEPT': '*/*', 'HTTP_HOST': '127.0.0.1:8000',
'HTTP_ACCEPT_ENCODING': 'gzip, deflate', 'HTTP_CONNECTION': 'keep-alive', 'wsgi.input': <_io.BufferedReader name=9>,
'wsgi.errors': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>, 'wsgi.version': (1, 0),
'wsgi.run_once': False, 'wsgi.url_scheme': 'http', 'wsgi.multithread': True, 'wsgi.multiprocess': False,
'wsgi.file_wrapper': <class 'wsgiref.util.FileWrapper'>}
5,通过域名获取IP地址
import socket def get_ip(domain): try: ip = socket.getaddrinfo(domain,'http')[0][4][0] except Exception as e: ip = "" return ip