当需要刷新缓存时候，就要登录腾讯云控制台去刷新，我觉的这就有点费事了。所以腾讯云也提供不同语言相应的SDK。经常远程服务器的话，在服务器上使用 [ 命令 + url ]方式直接刷新缓存多方便（使用这种方式，要保证你的id和key的安全，不要泄露了！） 首先登录腾讯云 -> 右上角头像 -> 访问管理 -> 访问秘钥 -> API秘钥管理 -> 生成下秘钥 -> ( ID和Key )python2和python3 都可以支持，我这里使用python3版本# 安装sdk包（python2和python3环境都存在的情况，python3使用pip3安装） pip3 install --upgrade tencentcloud-sdk-python pip3 install tencentcloud-sdk-python-cdnpython脚本，来自官方的代码，我添加了sys模块，可以使用传参的方式#!/bin/env python3 import sys import json from tencentcloud.common import credential from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.common.profile.http_profile import HttpProfile from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException from tencentcloud.cdn.v20180606 import cdn_client, models try: cred = credential.Credential("你的id", "你的key") httpProfile = HttpProfile() httpProfile.endpoint = "cdn.tencentcloudapi.com" clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile client = cdn_client.CdnClient(cred, "", clientProfile) req = models.PurgeUrlsCacheRequest() params = { "Urls": sys.argv[1:] } req.from_json_string(json.dumps(params)) resp = client.PurgeUrlsCache(req) print(resp.to_json_string()) except TencentCloudSDKException as err: print(err) 执行刷新缓存，效果和控制台一样，也会有域名判断的，在刷新记录中也可以看到执行命令刷新的记录可以用alias去做一个命令别名，这样不用每次都输python3 cdn.py 这个了，多个url刷新的话直接for循环# 写入系统环境变量（所有用户都可以执行cdn这个命令），也可以写入用户的环境配置文件中，只对单个用户生效 echo "alias cdn='python3 /script/cdn.py'" >>/etc/profile # 生效 source /etc/profile
pip 安装错误 Command "python setup.py egg_info" failed with error code
[python2.7版本] pip安装包 或者 pip升级pip版本出现此错误： ······ File "/tmp/pip-build-vD3Ntt/pip/setup.py", line 7 def read(rel_path: str) -> str: ^ SyntaxError: invalid syntax ---------------------------------------- Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-vD3Ntt/pip/ You are using pip version 8.1.2, however version 21.2.3 is available. You should consider upgrading via the 'pip install --upgrade pip' command. 出现此错误后，执行升级pip和setuptools也出同样错误，网上搜索此错误解决方式都是升级pip和setuptools 原因呢肯定是pip问题了，解决办法：# 本站下载 https://wojc.cn/sofw/get-pip.zip wget https://bootstrap.pypa.io/pip/2.7/get-pip.py python get-pip.py pip install --upgrade setuptools
linux命令行界面使用Python+Selenium+Chrome对网页截图保存(js渲染后的页面) 程序版本： 系统：centos7 python:3.6 Selenium:3.141 chrome|chromedriver:88.0.4324.96 chrome版本下载：Linux Google Chrome chromedriver版本下载：chromedriver 程序安装：# python3安装略过 # Selenium安装 pip3 install Selenium # 安装chrome wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm yum install google-chrome-stable_current_x86_64.rpm -y # 下载chromedriver(先确认安装的chrome版本，再下载对应的chromedriver版本) #查看版本 [root@localhost ~]#rpm -qa|grep google-chrome google-chrome-stable-88.0.4324.96-1.x86_64 #下载对应版本chromedriver wget http://chromedriver.storage.googleapis.com/88.0.4324.96/chromedriver_linux64.zip unzip chromedriver_linux64.zip mv chromedriver /usr/local/bin/ python代码： #!/bin/env python3 from selenium import webdriver from selenium.webdriver.chrome.options import Options import time options=webdriver.ChromeOptions() # 浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败 options.add_argument('--headless') # 谷歌文档提到需要加上这个属性来规避bug options.add_argument('--disable-gpu') # 取消沙盒模式 options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') # 指定浏览器分辨率 options.add_argument('window-size=1920x1080') driver=webdriver.Chrome(options=options) #网页地址 driver.get('https://www.baidu.com') #等待2秒再截图，如果网页渲染的慢截图的内容会有问题 #time.sleep(2) #截图 driver.get_screenshot_as_file('/tmp/baidu.png') #退出 driver.close() 执行脚本截图的图片乱码： # 解决文字乱码 把windows系统下微软雅黑字体拷贝到linux系统下 # windows字体目录 C:\Windows\Fonts\微软雅黑\MSYH.TTC # linux字体目录 /usr/share/fonts 再次查看网页截图图片：
warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless
使用selenium+PhantomJS保存网页快照时的警告信息，是因为selenium已经不支持PhantomJS了虽然有此警告提醒，但是selenium+PhantomJS还是可以正常保存截图用的 警告输出信息： /usr/python3/lib/python3.6/site-packages/selenium/webdriver/phantomjs/webdriver.py:49: UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless ' 解决： 改用Firefox或者Chrome的headless模式就行了
在编译安装python3时候如果没有安装openssl，编译安装后使用就会报错ImportError: No module named _ssl openssl-1.0.* 和 1.1.*版本， 二选一安装 openssl-1.0.2版本 #查看默认的openssl版本比较老了 [root@localhost ~]# openssl version OpenSSL 1.0.2k-fips 26 Jan 2017 #安装openssl-1.0.2u版本 [root@localhost ~]# wget https://www.openssl.org/source/old/1.0.2/openssl-1.0.2u.tar.gz [root@localhost ~]# tar xf openssl-1.0.2u.tar.gz [root@localhost ~]# cd openssl-1.0.2u [root@localhost openssl-1.0.2u]# ./config [root@localhost openssl-1.0.2u]# make && make install # python添加ssl支持编译时候可能出现的错误：/usr/local/ssl/lib/libssl.a: error adding symbols: Bad value。 # 请在openssl编译安装config时加-fPIC参数 # 在生成一个动态库时需要指定-fPIC，这是创建动态库所要求的，共享库被加载是在内存中的位置是不固定的，是一个相对的位置。 # 那么在生成静态库时通常不指定-fPIC, 可是在64bit编译使用静态库就会提示需要-fPIC重新编译该库。由于openssl编译静态库 # 时，没有使用-fPIC选项，使得编译出来的静态库没有重定位能力。这样在64bit机器上编译出来的静态库如果不指定-fPIC选项几乎 # 全部不能使用。因此需要重新加上-fPIC从新编译openssl #配置新版本生效（记得修改路径） [root@localhost ~]# echo 'export PATH=$PATH:/usr/local/ssl/bin' >> /etc/profile && source /etc/profile #配置 openssl 共享库 echo '/usr/local/ssl/lib' >>/etc/ld.so.conf && ldconfig [root@localhost ~]# which openssl /usr/local/ssl/bin/openssl [root@localhost ~]# openssl version OpenSSL 1.0.2u 20 Dec 2019 openssl-1.1*版本 #安装使用openssl-1.1.1g版本 wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz tar xf openssl-1.1.1g.tar.gz cd openssl-1.1.1g ./config --prefix=/usr/local/openssl-1.1.1g make && make install #配置新版本生效(记得替换路径，我这里用*表示) [root@localhost ~]# echo 'export PATH=$PATH:/usr/local/openssl-1.1.*/bin' >> /etc/profile && source /etc/profile #配置 openssl 共享库路径(记得替换路径，我这里用*表示) echo '/usr/local/openssl-1.1.*/lib' >>/etc/ld.so.conf && ldconfig 重新安装python3 #只需要将python的安装目录删除即可 [root@localhost ~]# rm -rf /usr/local/python38/ #重新解压原来的源码包 [root@localhost ~]# tar xf Python-3.8.2.tar.tgz [root@localhost ~]# cd Python-3.8.2 ###configure注意 #如果使用openssl-1.0.*的版本，configure配置： [root@localhost Python-3.8.2]# vim Modules/Setup #删除ssl的注释 ······ # Socket module helper for SSL support; you must comment out the other # socket line above, and possibly edit the SSL variable: SSL=/usr/local/ssl _ssl _ssl.c \ -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ -L$(SSL)/lib -lssl -lcrypto [root@localhost Python-3.8.2]# ./configure --prefix=/usr/local/python38 --enable-shared #如果使用openssl-1.1.*的版本，configure配置： [root@localhost Python-3.8.2]# ./configure --prefix=/usr/local/python38 --enable-shared --with-openssl=/usr/local/openssl-1.1.* #编译/安装 [root@localhost Python-3.8.2]# make [root@localhost Python-3.8.2]# make install python3编译报错： #如果安装1.1.0还有1.1.1版本，config没有指定安装目录python编译的时候会报错，要么就是make的最后报下面的错 Following modules built successfully but were removed because they could not be imported: _hashlib _ssl Could not build the ssl module! Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host(). LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381 查看ssl模块是否支持 #确认ssl模块 [root@localhost Python-3.8.2]# python3 Python 3.8.2 (default, May 16 2020, 03:09:38) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import ssl >>>
virtualenvwrapper是Ian Bicking的virtualenv工具的一组扩展。扩展包括用于创建和删除虚拟环境以及管理开发工作流程的包装程序，从而使一次处理多个项目变得更加容易，而不会在其依赖项中引入冲突。 特征 1、在一个地方组织所有虚拟环境 2、包装程序，用于管理您的虚拟环境（创建，删除，复制） 3、使用单个命令在环境之间切换 4、使用虚拟环境作为参数的命令的制表符完成 5、所有操作的用户可配置钩子 6、用于创建更多可共享扩展的插件系统 如果用virtualenv 每次切换虚拟环境就要source之前虚拟环境bin目录，如果有很多就需要都记住这些目录,所以就用virtualenvwrapper来统一管理，而且省去了每次都要切换的麻烦 安装使用（python2版本） #centos默认没有pip命令，需要安装 [root@localhost ~]# wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo [root@localhost ~]# yum install python-pip -y [root@localhost ~]# pip install --upgrade pip #安装virtualenvwrapper [root@localhost ~]# pip install virtualenvwrapper #确认下文件路径 [root@localhost ~]# which python /usr/bin/python [root@localhost ~]# find / -name virtualenvwrapper.sh /usr/bin/virtualenvwrapper.sh #添加配置 [root@localhost ~]# vim ~/.bashrc ······ #添加配置 WORKON_HOME=~/.virtualenvs #虚拟环境统一管理目录 VIRTUALENVWRAPPER_PYTHON=/usr/bin/python #虚拟环境对应的python版本 source /usr/bin/virtualenvwrapper.sh #执行virtualenvwrapper安装脚本 #生效配置 [root@localhost ~]# source ~/.bashrc virtualenvwrapper.user_scripts creating /root/.virtualenvs/initialize virtualenvwrapper.user_scripts creating /root/.virtualenvs/premkvirtualenv virtualenvwrapper.user_scripts creating /root/.virtualenvs/postmkvirtualenv virtualenvwrapper.user_scripts creating /root/.virtualenvs/prermvirtualenv virtualenvwrapper.user_scripts creating /root/.virtualenvs/postrmvirtualenv virtualenvwrapper.user_scripts creating /root/.virtualenvs/predeactivate virtualenvwrapper.user_scripts creating /root/.virtualenvs/postdeactivate virtualenvwrapper.user_scripts creating /root/.virtualenvs/preactivate virtualenvwrapper.user_scripts creating /root/.virtualenvs/postactivate virtualenvwrapper.user_scripts creating /root/.virtualenvs/get_env_details virtualenvwrapper.user_scripts creating /root/.virtualenvs/premkproject virtualenvwrapper.user_scripts creating /root/.virtualenvs/postmkproject #创建虚拟环境 [root@localhost ~]# mkvirtualenv test created virtual environment CPython2.7.5.final.0-64 in 695ms creator CPython2Posix(dest=/root/.virtualenvs/test, clear=False, global=False) seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/root/.local/share/virtualenv/seed-app-data/v1.0.1) activators PythonActivator,CShellActivator,FishActivator,PowerShellActivator,BashActivator virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/predeactivate virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/postdeactivate virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/preactivate virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/postactivate virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/get_env_details (test) [root@localhost ~]# deactivate [root@localhost ~]# mkvirtualenv test-1 created virtual environment CPython2.7.5.final.0-64 in 379ms creator CPython2Posix(dest=/root/.virtualenvs/test-1, clear=False, global=False) seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/root/.local/share/virtualenv/seed-app-data/v1.0.1) activators PythonActivator,CShellActivator,FishActivator,PowerShellActivator,BashActivator virtualenvwrapper.user_scripts creating /root/.virtualenvs/test-1/bin/predeactivate virtualenvwrapper.user_scripts creating /root/.virtualenvs/test-1/bin/postdeactivate virtualenvwrapper.user_scripts creating /root/.virtualenvs/test-1/bin/preactivate virtualenvwrapper.user_scripts creating /root/.virtualenvs/test-1/bin/postactivate virtualenvwrapper.user_scripts creating /root/.virtualenvs/test-1/bin/get_env_details (test-1) [root@localhost ~]# deactivate #切换虚拟环境 [root@localhost ~]# workon test (test) [root@localhost ~]# deactivate [root@localhost ~]# workon test-1 (test-1) [root@localhost ~]# deactivate 安装使用（python3版本
DjangoBlog项目 Github 详情 部署教程 我搭建好的演示地址 我这里记录下我参考作者的文章部署的过程，mysql和python3和nginx环境已经ok的 环境准备： 1、Centos7 2、Mysql5.7 3、Python3.8 4、Nginx1.18 5、python虚拟环境目录 /python/env 6、DjangoBlog源码目录 /python/DjangoBlog 部署开始#安装memcached和supervisor yum install memcached supervisor -y #动态链接库文件，添加python3和mysql的，我这里是源码安装的所以这里要加上，不然系统找不到 vim /etc/ld.so.conf include ld.so.conf.d/*.conf /usr/python3/lib /data/mysql/lib #设置python虚拟环境 pip3 install virtualenv mkdir -p /python/env && cd /python/env [root@localhost env]# virtualenv -p /usr/python3/bin/python3 djangoblog created virtual environment CPython3.8.2.final.0-64 in 356ms creator CPython3Posix(dest=/python/env/djangoblog, clear=False, global=False) seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/root/.local/share/virtualenv/seed-app-data/v1.0.1) activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator #进入python3虚拟环境 [root@localhost env]# source djangoblog/bin/activate (djangoblog) [root@localhost env]# #安装djangoblog运行所需要的依赖 (djangoblog) [root@localhost env]# cd /python/DjangoBlog/ (djangoblog) [root@localhost DjangoBlog]# pip3 install -Ur requirements.txt #这里遇到2个错误因为版本问题，修改下requirements.txt，文件中对应的修改成下面的然后再次执行 django-mdeditor==0.1.17.1 wrapt==1.11.0 mysql数据库配置 #创建数据库和授权 CREATE USER 'djangoblog'@'localhost' IDENTIFIED BY '123456'; CREATE DATABASE `djangoblog` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */; GRANT all ON djangoblog.* TO 'djangoblog'@'localhost'; FLUSH PRIVILEGES; #修改djangoblog配置文件 (djangoblog) [root@localhost DjangoBlog]# vim DjangoBlog/settings.py #这里我用mysql.sock连接，用localhost有问题，之前我部署的用localhost没问题 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'djangoblog', 'USER': 'djangoblog', 'PASSWORD': '123456', 'HOST': '/data/mysql/mysql.sock', 'PORT': 3306, 'OPTIONS': {'charset': 'utf8mb4'}, } } #执行数据库迁移 (djangoblog) [root@localhost DjangoBlog]# ./manage.py makemigrations Migrations for 'owntracks': owntracks/migrations/0001_initial.py - Create model OwnTrackLog Migrations for 'servermanager': servermanager/migrations/0001_initial.py - Create model commands - Create model EmailSendLog Migrations for 'accounts': accounts/migrations/0001_initial.py - Create model BlogUser Migrations for 'blog': blog/migrations/0001_initial.py - Create model BlogSettings - Create model Links - Create model SideBar - Create model Tag - Create model Category - Create model Article Migrations for 'comments': comments/migrations/0001_initial.py - Create model Comment Migrations for 'oauth': oauth/migrations/0001_initial.py - Create model OAuthConfig - Create model OAuthUser (djangoblog) [root@localhost DjangoBlog]# ./manage.py migrate Operations to perform: Apply all migrations: accounts, admin, auth, blog, comments, contenttypes, oauth, owntracks, servermanager, sessions, sites Running migrations: Applying contenttypes.0001_initial... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0001_initial... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying auth.0008_alter_user_username_max_length... OK Applying auth.0009_alter_user_last_name_max_length... OK Applying auth.0010_alter_group_name_max_length... OK Applying auth.0011_update_proxy_permissions... OK Applying accounts.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying admin.0003_logentry_add_action_flag_choices... OK Applying blog.0001_initial... OK Applying comments.0001_initial... OK Applying oauth.0001_initial... OK Applying owntracks.0001_initial... OK Applying servermanager.0001_initial... OK Applying sessions.0001_initial... OK Applying sites.0001_initial... OK Applying sites.0002_alter_domain_unique... OK (djangoblog) [root@localhost DjangoBlog]# ./manage.py createsuperuser 用户名: admin 电子邮件地址: admin@admin.com Password: Password (again): [2020-05-03 23:51:12,686] INFO [DjangoBlog.utils.news:63 utils] cache_decorator set cache:get_current_site key:4727341a8b52dd69399fc48a8a529c59 [2020-05-03 23:51:12,822] INFO [DjangoBlog.spider_notify.baidu_notify:30 spider_notify] {"remain":100000,"success":0,"not_same_site":["https://example.com/author/admin.html"]} Superuser created successfully. (djangoblog) [root@localhost DjangoBlog]# ./manage.py collectstatic --no-input 746 static files copied to '/python/env/DjangoBlog/collectedstatic'. (djangoblog) [root@localhost DjangoBlog]# ./manage.py compress --force Compressing... done Compressed 4 block(s) from 26 template(s) for 0 context(s). #启动测试，出现访问地址说明就ok的 (djangoblog) [root@localhost DjangoBlog]# ./manage.py runserver Watching for file changes with StatReloader [2020-05-03 23:52:04,067] INFO [django.utils.autoreload.run_with_reloader:598 autoreload] Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). May 03, 2020 - 23:52:04 Django version 3.0.3, using settings 'DjangoBlog.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. gunicorn 配置 #安装gunicorn (djangoblog) [root@localhost DjangoBlog]# pip3 install gunicorn (djangoblog) [root@localhost DjangoBlog]# vim /python/gunicorn_start.sh #!/bin/bash NAME="DjangoBlog" DJANGODIR=/python/DjangoBlog #Django project directory USER=server # the user to run as GROUP=server # the group to run as NUM_WORKERS=1 # how many worker processes should Gunicorn spawn DJANGO_SETTINGS_MODULE=DjangoBlog.settings # which settings file should Django use DJANGO_WSGI_MODULE=DjangoBlog.wsgi # WSGI module name echo "Starting $NAME as `whoami`" # Activate the virtual environment cd $DJANGODIR source /python/env/djangoblog/bin/activate export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE export PYTHONPATH=$DJANGODIR:$PYTHONPATH # Create the run directory if it doesn't exist RUNDIR=$(dirname $SOCKFILE) test -d $RUNDIR || mkdir -p $RUNDIR # Start your Django Unicorn # Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon) exec /python/env/djangoblog/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \ --name $NAME \ --workers $NUM_WORKERS \ --user=$USER --group=$GROUP \ --log-level=debug \ --log-file=- (djangoblog) [root@localhost DjangoBlog]# chmod +x /python/gunicorn_start.sh (djangoblog) [root@localhost DjangoBlog]# cd /python (djangoblog) [root@localhost python]# ./gunicorn_start.sh Starting DjangoBlog as root dirname: missing operand Try 'dirname --help' for more information. [2020-05-03 23:59:35 +0800] [69002] [DEBUG] Current configuration: config: None bind: ['127.0.0.1:8000'] backlog: 2048 workers: 1 worker_class: sync threads: 1 worker_connections: 1000 max_requests: 0 max_requests_jitter: 0 timeout: 30 graceful_timeout: 30 keepalive: 2 limit_request_line: 4094 limit_request_fields: 100 limit_request_field_size: 8190 reload: False reload_engine: auto reload_extra_files: [] spew: False check_config: False preload_app: False sendfile: None reuse_port: False chdir: /python/DjangoBlog daemon: False raw_env: [] pidfile: None worker_tmp_dir: None user: 0 group: 0 umask: 0 initgroups: False tmp_upload_dir: None secure_scheme_headers: {'X-FORWARDED-PROTOCOL': 'ssl', 'X-FORWARDED-PROTO': 'https', 'X-FORWARDED-SSL': 'on'} forwarded_allow_ips: ['127.0.0.1'] accesslog: None disable_redirect_access_to_syslog: False access_log_format: %(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" errorlog: - loglevel: debug capture_output: False logger_class: gunicorn.glogging.Logger logconfig: None logconfig_dict: {} syslog_addr: udp://localhost:514 syslog: False syslog_prefix: None syslog_facility: user enable_stdio_inheritance: False statsd_host: None dogstatsd_tags: statsd_prefix: proc_name: DjangoBlog default_proc_name: DjangoBlog.wsgi:application pythonpath: None paste: None on_starting: <function OnStarting.on_starting at 0x7fb564ee1b80> on_reload: <function OnReload.on_reload at 0x7fb564ee1ca0> when_ready: <function WhenReady.when_ready at 0x7fb564ee1dc0> pre_fork: <function Prefork.pre_fork at 0x7fb564ee1ee0> post_fork: <function Postfork.post_fork at 0x7fb564ef2040> post_worker_init: <function PostWorkerInit.post_worker_init at 0x7fb564ef2160> worker_int: <function WorkerInt.worker_int at 0x7fb564ef2280> worker_abort: <function WorkerAbort.worker_abort at 0x7fb564ef23a0> pre_exec: <function PreExec.pre_exec at 0x7fb564ef24c0> pre_request: <function PreRequest.pre_request at 0x7fb564ef25e0> post_request: <function PostRequest.post_request at 0x7fb564ef2670> child_exit: <function ChildExit.child_exit at 0x7fb564ef2790> worker_exit: <function WorkerExit.worker_exit at 0x7fb564ef28b0> nworkers_changed: <function NumWorkersChanged.nworkers_changed at 0x7fb564ef29d0> on_exit: <function OnExit.on_exit at 0x7fb564ef2af0> proxy_protocol: False proxy_allow_ips: ['127.0.0.1'] keyfile: None certfile: None ssl_version: 2 cert_reqs: 0 ca_certs: None suppress_ragged_eofs: True do_handshake_on_connect: False ciphers: None raw_paste_global_conf: [] strip_header_spaces: False [2020-05-03 23:59:35 +0800] [69002] [INFO] Starting gunicorn 20.0.4 [2020-05-03 23:59:35 +0800] [69002] [DEBUG] Arbiter booted [2020-05-03 23:59:35 +0800] [69002] [INFO] Listening at: http://127.0.0.1:8000 (69002) [2020-05-03 23:59:35 +0800] [69002] [INFO] Using worker: sync [2020-05-03 23:59:35 +0800] [69002] [DEBUG] 1 workers [2020-05-03 23:59:35 +0800] [69008] [INFO] Booting worker with pid: 69008 Nginx 配置 #修改nginx配置文件 nginx.conf server { listen 80; server_name www.djangoblog.com; root /python/DjangoBlog/; access_log logs/django_access.log; error_log logs/django_error.log; location /static/ { alias /python//DjangoBlog/collectedstatic/; expires max; access_log off; log_not_found off; } location /media { # 静态文件配置 alias /python/DjangoBlog/uploads/; expires max; } location ~ \.py$ { return 403; } location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_redirect off; if (!-f $request_filename) { proxy_pass http://127.0.0.1:8000; break; } } } #重启nginx nginx -s reload Supervisor 配置 (djangoblog) [root@localhost ~]# vim /etc/supervisord.d/djangoblog.ini [program:djangoblog] command = /python/gunicorn_start.sh user = root autostart=true autorestart=true redirect_stderr = true stdout_logfile = /var/log/djangoblog.log stderr_logfile=/var/log/djangoblog.err (djangoblog) [root@localhost ~]# supervisord -c /etc/supervisord.conf (djangoblog) [root@localhost ~]# supervisorctl update (djangoblog) [root@localhost ~]# supervisorctl reload Restarted supervisord (djangoblog) [root@localhost ~]# systemctl start memcached 访问站点
Linux下python3.x安装，之前没有写下来，这次完整记录下 使用Centos7为例，其他的大同小异 先安装依赖，避免后面有模块不能导入问题 wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo #此包不装，python解释器里面用不了删除键等 yum install readline-devel -y #安装python3前最好先安装好openssl，不然使用中会提示没有ssl模块: yum install openssl openssl-devel -y #或者源码编译安装最其他版本的openssl，yum安装的版本比较早了 #Python3添加支持ssl模块 ：https://wojc.cn/archives/584.html #其他的包（安装这么多是为了后面使用不会缺少各种模块） yum install gcc gcc-c++ zlib-devel libuuid-devel ncurses-devel \ sqlite-devel tkinter libuuid-devel pcre-devel libffi-devel libpcap-devel \ tk-devel tcl-devel python-sqlite3dbm gdbm-devel db4-devel xz-devel bzip2-devel -y 下载python3.8 源码包 Python Download #解压安装 tar xf Python-3.8.2.tgz cd Python-3.8.2 # --enable-shared 启用共享，方便其他依赖python的一些内置库（比如 mysqlclient) 的资源的正常安装 # --enable-optimizations 是优化选项(LTO,PGO等)加上这个flag编译后，性能有10%左右的优化，但是编译时间较久 ./configure --prefix=/usr/local/python38 --enable-shared #make这里可以使用make test查看缺少什么模块，不过非常耗时间 make && make install 配置环境变量生效 echo 'export PATH=$PATH:/usr/local/python38/bin' >>/etc/profile && source /etc/profile #添加动态链接库（不添加会报错libpython3.8.so.1.0） echo '/usr/local/python38/lib' >>/etc/ld.so.conf && ldconfig 确认版本 [root@localhost ~]# which python3 /usr/local/python38/bin/python3 [root@localhost ~]# python3 -V Python 3.8.2 #pip3版本 [root@localhost ~]# pip3 -V pip 19.2.3 from /usr/local/python38/lib/python3.8/site-packages/pip (python 3.8) [root@localhost ~]# python3 Python 3.8.2 (default, May 3 2020, 08:12:25) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> print (sys.version) 3.8.2 (default, May 3 2020, 08:12:25) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] >>>
pip和easy_instal 更换国内阿里云源，加速下载 更换pip源 #/root目录下创建一个.pip目录 [root@localhost ~]# mkdir /root/.pip [root@localhost ~]# vim /root/.pip/pip.conf #内容如下： [global] index-url=http://mirrors.aliyun.com/pypi/simple/ [install] trusted-host=mirrors.aliyun.com更换easy_install源 #/root目录下创建一个.pydistutils.cfg文件 [root@localhost ~]# vim .pydistutils.cfg #内容如下： [easy_install] index-url=http://mirrors.aliyun.com/pypi/simple/ find-links=http://mirrors.aliyun.com/pypi/simple/
脚本定时发送微博热搜关键词到微信企业号
shell脚本获取，python脚本发送。此篇文章需要有企业微信管理权限才能使用 1、企业微信（企业号）现在申请要企业验证，我申请的比较早当时不需要验证2、crontab计划任务3、python4、个人微信加入企业中，个人微信关注企业号，然后把人员加入到部门，消息按部门发送 效果展示(因为微信API接口文本消息内容，最长不超过2048个字节，超过将截断）所以分四条发送 url-utf8.py url编码解码脚本 #!/bin/python # -*- coding: utf-8 -*- import urllib import sys rawurl = str(sys.argv[1]) url = urllib.unquote(rawurl) print url num.txt 发送消息每条消息取多少行 1,12p 13,25p 26,37p 38,50p wxweibo.py 发送文本内容到企业微信应用的脚本 #!/usr/bin/python #_*_coding:utf-8 _*_ __author__ = 'lvnian' import urllib,urllib2 import json import sys reload(sys) sys.setdefaultencoding('utf8') def gettoken(corpid,corpsecret): gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret try: token_file = urllib2.urlopen(gettoken_url) except urllib2.HTTPError as e: print e.code print e.read().decode("utf8") sys.exit() token_data = token_file.read().decode('utf-8') token_json = json.loads(token_data) token_json.keys() token = token_json['access_token'] return token def senddata(access_token,user,content): send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token send_values = { "touser":user , #企业号中的用户帐号，我这里按部门发送的，所以这个参数填错没关系。 "toparty":"3", #企业号中的部门id "msgtype":"text", #发送的消息类型。 "agentid":"1000003", #企业号中应用的AgentId "text":{ "content":content }, "safe":"0" } send_data = json.dumps(send_values, ensure_ascii=False) send_request = urllib2.Request(send_url, send_data) response = json.loads(urllib2.urlopen(send_request).read()) print str(response) if __name__ == '__main__': user = str(sys.argv[1]) #第一个参数(第一个和第二参数没实际作用,但不能为空) subject = str(sys.argv[2]) #第二个参数 content = str(sys.argv[3]) #第三个参数(需要发送的消息内容) corpid = '企业微信管理-我的企业-就可看到此id' #CorpID是企业微信(原企业号)的标识 corpsecret = '企业微信管理-应用管理-打开对应应用就可看到此参数' #需要发送的应用Secret accesstoken = gettoken(corpid,corpsecret) senddata(accesstoken,user,content) wxweibo.sh 执行脚本 #!/bin/bash date=`date +%H:%M` #下载热搜关键词前50页面并过滤热搜词和url cd /tmp/weibo/ && wget -q https://s.weibo.com/top/summary/ grep " /tmp/weibo/aa && cp aa ab #对url编码解码并替换原来url for i in `awk -F "[=|&]" '{print $3}' aa` do wz="`python /tmp/weibo/url-utf8.py $i`" sed -i "s/$i/{$wz}/" ab done #删除和替换字符导出结果 sed 's/{//;s/}//;s#\/weibo#https://s.weibo.com#g;s/com/com\/weibo/g;s/#/%23/g;s/ //g;s/<a/<a /' ab >resou.txt #分4次发送热搜词到公众号消息(1条消息限制最长2048个字节) for i in `cat /tmp/weibo/num.txt` do nub=`echo "$i"|sed 's/p//'|tr "," "-"` /bin/python /tmp/weibo/wxweibo.py 0 0 "[$date]微博热搜排名$nub关键词: `sed -n $i /tmp/weibo/resou.txt`" done #删除临时文件 rm -rf index.html* aa ab wxweibo.sh 执行 sh /tmp/weibo/wxweibo.sh {u'invaliduser': u'0', u'errcode': 0, u'errmsg': u'ok'} {u'invaliduser': u'0', u'errcode': 0, u'errmsg': u'ok'} {u'invaliduser': u'0', u'errcode': 0, u'errmsg': u'ok'} {u'invaliduser': u'0', u'errcode': 0, u'errmsg': u'ok'} crontab 每小时0分定时发送 00 * * * * /tmp/weibo/wxweibo.sh 百度搜热也可以这样做,效果展示；因为百度链接长所以分5条消息发送