Python常用命令和工具整理

爬虫
数据分析
运维自动化开发

==================================================================================
python虚拟环境配置

1, 单虚拟化环境配置
python3 -m venv py3

2. 多虚拟化环境配置及管理
使用工具: virtualenv, virtualenvwrapper
# 安装 virtualenv
pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ virtualenv
# 安装 virtualenvwrapper --- 这是对 virtualenv 的封装版本,一定要在virtualenv后安装
pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ virtualenvwrapper

创建虚拟环境:
选择一个用来存放虚拟环境的文件,如 C:/python
cd C:\python
virtualenv opsvenv # 创建一个名字为 opsvenv 的虚拟环境
C:\Python>cd opsvenv
C:\Python\opsvenv>dir
驱动器 C 中的卷没有标签。
卷的序列号是 DA89-AAED

C:\Python\opsvenv 的目录

2019/12/17 09:38 <DIR> .
2019/12/17 09:38 <DIR> ..
2019/11/13 15:18 <DIR> Include
2019/12/17 09:38 <DIR> Lib
2018/12/24 00:22 30,342 LICENSE.txt
2019/12/17 09:40 <DIR> Scripts
2019/12/17 09:38 <DIR> tcl
1 个文件 30,342 字节
6 个目录 30,755,811,328 可用字节

注意:如果不识别 virtualenv 命令,可能是python安装路径没添加到系统环境变量或没安装virtualenv或没有重新打开一个cmd窗口;

启动虚拟环境
# 进入虚拟环境文件
cd C:\Python\opsvenv\Scripts>
# 启动虚拟环境
activate

# 安装 django-2.2
pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ django==2.2.8

(opsvenv) C:\Python\opsvenv\Scripts>pip list
Package Version
---------- -------
Django 2.2.8
pip 19.3.1
pytz 2019.3
setuptools 42.0.2
sqlparse 0.3.0
wheel 0.33.6

# 退出虚拟环境
deactivate

# 创建虚拟环境
cd ~/.virtualenvs
mkvirtualenv GP3 -p /usr/bin/python3

==================================================================================

# 安装ansible指定版本, 从清华大学python源
pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ django==1.11.29

pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ djangorestframework==3.9.4

pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ pymysql
pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ django-rest-swagger

 

pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ django==2.2.8

pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ ansible==2.7.14

pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ setuptools==39.0.1
pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ paramiko==2.4.1
pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ PyYAML==5.1.2
pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ jinja2==2.10.3
pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ cryptography==2.8
pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ MarkupSafe==1.1.1

# 从国内python源安装最新版本flask
pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ flask

# 安装最牛逼的python框架 sanic
pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ sanic

安装软件指定python源:
pip3 install -i https://mirrors.zte.com.cn/pypi/simple flask --trusted-host mirrors.zte.com.cn


# 最牛逼的爬虫框架
pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ scrapy


sanic中文文档:
https://www.osgeo.cn/sanic/
https://sanic-cn.readthedocs.io/zh/latest/index.html
https://github.com/chinesejar/sanic-cn

 

python -m pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ --upgrade pip


Django2.2 安装rest-framework
https://django-rest-swagger.readthedocs.io/en/latest/
版本:
Django 2.2.11
djangorestframework 3.10.0

pip uninstall djangorestframework
pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ Django==2.2.11
pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ djangorestframework==3.9.4

pip install django-filter
pip install markdown


替代品:
pip install -U drf-yasg
https://drf-yasg.readthedocs.io/en/stable/

==================================================================================

国内可用的python源:
清华大学:
https://pypi.tuna.tsinghua.edu.cn/simple/

阿里云:
http://mirrors.aliyun.com/pypi/simple/

豆瓣:
http://pypi.douban.com/simple/

中国科学技术大学:
http://pypi.mirrors.ustc.edu.cn/simple/

==================================================================================

Django Rest Swagger生成api文档

关于swagger
Swagger能成为最受欢迎的REST APIs文档生成工具之一,有以下几个原因:
- Swagger 可以生成一个具有互动性的API控制台,开发者可以用来快速学习和尝试API。
- Swagger 可以生成客户端SDK代码用于各种不同的平台上的实现。
- Swagger 文件可以在许多不同的平台上从代码注释中自动生成。
- Swagger 有一个强大的社区,里面有许多强悍的贡献者。

下面就实战django rest swagger为drf生成api接口文档

环境
Python3.6
Django1.11
django-rest-swagger
djangorestframework
安装
pip install django==1.11.6
pip instal djangorestframework
pip install django-rest-swagger


创建项目和app
startproject apitest
startapp api


配置 rest_api/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# 加入以下
'api',
'rest_framework',
'rest_framework_swagger',

]
# swagger 配置项
SWAGGER_SETTINGS = {
# 基础样式
'SECURITY_DEFINITIONS': {
"basic":{
'type': 'basic'
}
},
# 如果需要登录才能够查看接口文档, 登录的链接使用restframework自带的.
'LOGIN_URL': 'rest_framework:login',
'LOGOUT_URL': 'rest_framework:logout',
# 'DOC_EXPANSION': None,
# 'SHOW_REQUEST_HEADERS':True,
# 'USE_SESSION_AUTH': True,
# 'DOC_EXPANSION': 'list',
# 接口文档中方法列表以首字母升序排列
'APIS_SORTER': 'alpha',
# 如果支持json提交, 则接口文档中包含json输入框
'JSON_EDITOR': True,
# 方法列表字母排序
'OPERATIONS_SORTER': 'alpha',
'VALIDATOR_URL': None,
}

 

配置api/serializer.py
# 序列化
from django.contrib.auth.models import User,Group
from rest_framework import serializers

class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = "__all__"
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model =Group
fields = "__all__"


配置api/views.py
# 视图
from django.contrib.auth.models import User,Group
from rest_framework import viewsets
from api.serializers import UserSerializer,GroupSerializer

# Create your views here.

class UserViewSet(viewsets.ModelViewSet):
'''查看,编辑用户的界面'''
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer

class GroupViewSet(viewsets.ModelViewSet):
'''查看,编辑组的界面'''
queryset = Group
serializer_class = GroupSerializer


配置apitest/urls.py

from django.conf.urls import url,include
from django.contrib import admin
from rest_framework import routers
from api import views

# 路由
router = routers.DefaultRouter()
router.register(r'users',views.UserViewSet,base_name='user')
router.register(r'groups',views.GroupViewSet,base_name='group')


# 重要的是如下三行
from rest_framework.schemas import get_schema_view
from rest_framework_swagger.renderers import SwaggerUIRenderer, OpenAPIRenderer
schema_view = get_schema_view(title='Users API', renderer_classes=[OpenAPIRenderer, SwaggerUIRenderer])


urlpatterns = [
# swagger接口文档路由
url(r'^docs/', schema_view, name="docs"),
url(r'^admin/', admin.site.urls),
url(r'^',include(router.urls)),
# drf登录
url(r'^api-auth/',include('rest_framework.urls',namespace='rest_framework'))
]

最终效果
drf自带的接口UI


==================================================================================

一些学习网站:
https://blog.51cto.com/784687488/2173727
通过 Ansible API 获取主机信息

django基础:

https://www.cnblogs.com/clschao/articles/10526431.html

posted @ 2020-04-06 10:59  冷水泡茶  阅读(824)  评论(0)    收藏  举报