python入门

开发环境安装

  1. 下载Python 可以选择安装版 Windows x86 executable installer
  2. 下载pycharm 我用的是pycharm professional 2018.2
  3. 打开python工程,自动识别,选择安装依赖。
  4. Run/Debug Configurations 配置启动入口文件

PIP更新

pip install –upgrade pip # python2.x linux
pip3 install –upgrade pip # python3.x linux
python -m pip install -U pip # python2.x windows
python -m pip3 install -U pip # python3.x windows

\Lib\site-packages 依赖的位置

打开pycharm→File→Setting→Project→Project Interpreter→pip

pycharm→File→Setting→Project→Project Interpreter→点击加号→ManageRepositories设置仓库地址:清华大学镜像 https://pypi.tuna.tsinghua.edu.cn/simple

依赖安装

新环境可以通过复制site-packages里面的包进行依赖包的拷贝。注意pyvenv.cfg等配置文件内容不需要替换

遇到问题

from werkzeug import secure_filename,FileStorage 报错cannot import name ‘FileStorage’

出现cannot import secure_filename把from werkzeug 改为from werkzeug.utils

改完出现cannot import name ‘FileStorage’ 分两行写:
from werkzeug.utils import secure_filename
from werkzeug.datastructures import FileStorage

基本语法

模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# support.py 模块:
def print_func( param ):
print "Hello : ", param
return

# 导入模块
import support

# 现在可以调用模块里包含的函数了
support.print_func("Run")

# from…import 语句
from modname import function1[, function2[, ... functionN]]

# python包
__init__.py 用于标识当前文件夹是一个包,里面可以有非常丰富的内容



安装第三方包

pip install -U socketIO-client

pycharm中:pycharm→File→Setting→Project→Project Interpreter→点击加号→搜索→Install Package

类、函数

函数(def)、 类(class)

1
2
# 入口
if __name__ == '__main__':

web开发

关闭tls1.0,tls1.1

写法非常多。其中一中写法:

1
2
3
4
5
6
7
import ssl

def start_https():
wsgi.server(eventlet.wrap_ssl(eventlet.listen((app_init.host, https_port), family=fam),
certfile='server.crt',
keyfile='server.key',
server_side=True, ssl_version=ssl.PROTOCOL_TLSv1_2,), app)

测试可以使用curl:

curl –tlsv1.0 https://192.168.38.152:8443
curl: (60) Peer’s certificate issuer has been marked as not trusted by the user.
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a “bundle”
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn’t adequate, you can specify an alternate file
using the –cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you’d like to turn off curl’s verification of the certificate, use
the -k (or –insecure) option.

curl –tlsv1.0 https://192.168.38.152:8443
curl: (35) Peer reports incompatible or unsupported protocol version.

重新协商 CVE-2011-1473

https://bugs.python.org/issue32257#msg307879
https://bugs.python.org/issue32257
https://blog.csdn.net/liuzk2014/article/details/89304483
https://github.com/python/cpython/pull/4763/commits/c9a7eda551b7fb8ab0dad7131e85f9722a3e405e#diff-f6439be9c66350dde4c35dbeea0352c96cc970ba12b0478f6ae36f10725bd8c5

测试:

1
2
3
openssl s_client -connect 192.168.38.152:8443
HEAD / HTTP/1.0
R

其他

call

如果在类中实现了 __call__ 方法,那么实例对象也将成为一个可调用对象

flask web框架

manage.py通过Run/Debug Configurations → Configuration Additional options:添加--host 0.0.0.0 --port 8088指定其他服务器可以访问以及端口

移植问题

--disable-ipv6 导致不支持IPV6,可能需要重新编译增加模块。可以通过family=socket.AF_INET6来限制监听v4还是v6。

参考