• python django 基本测试 及调试 201812


    #####20181225

    1.
    python解决SNIMissingWarning和InsecurePlatformWarning警告
    在想要获取https站点的资源时,会报出SNIMissingWarning和InsecurePlatformWarning警告

    SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform.

    This may cause the server to present an incorrect TLS certificate, which can cause validation failures. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning.
    SNIMissingWarning
    A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
    InsecurePlatformWarning


    解决方法:

    在cmd中输入:
    pip install pyopenssl ndg-httpsclient pyasn1

    2.在安装python的pip工具时,遇到以下报错[Errno -3] Temporary failure

    https://www.aliyun.com/jiaocheng/519493.html
    以上问题发现原来是DNS配置错误,域名服务器地址是本机IP地址,127.0.0.1,所以修改域名服务器


    sudo gedit /etc/resolv.conf

    nameserver 8.8.8.8

    3.

    /usr/local/lib/python2.7/site-packages/cryptography/hazmat/primitives/constant_time.py:26: CryptographyDeprecationWarning: Support for your Python version is deprecated. The next version of cryptography will remove support. Please upgrade to a 2.7.x release that supports hmac.compare_digest as soon as possible.


    解决方法
    1.pip2.7 install cryptography==2.7

    Collecting cryptography==2.7
    Could not find a version that satisfies the requirement cryptography==2.7 (from versions: 0.1, 0.2, 0.2.1, 0.2.2, 0.3, 0.4, 0.5, 0.5.1, 0.5.2, 0.5.3, 0.5.4, 0.6, 0.6.1, 0.7, 0.7.1, 0.7.2, 0.8, 0.8.1, 0.8.2, 0.9, 0.9.1, 0.9.2, 0.9.3, 1.0, 1.0.1, 1.0.2, 1.1, 1.1.1, 1.1.2, 1.2, 1.2.1, 1.2.2, 1.2.3, 1.3, 1.3.1, 1.3.2, 1.3.3, 1.3.4, 1.4, 1.5, 1.5.1, 1.5.2, 1.5.3, 1.6, 1.7, 1.7.1, 1.7.2, 1.8, 1.8.1, 1.8.2, 1.9, 2.0, 2.0.1, 2.0.2, 2.0.3, 2.1, 2.1.1, 2.1.2, 2.1.3, 2.1.4, 2.2, 2.2.1, 2.2.2, 2.3, 2.3.1, 2.4, 2.4.1, 2.4.2)
    No matching distribution found for cryptography==2.7


    2.
    or upgrade python2.7.(new)

    3.use pyenv
    (使用 pyenv,cache 目录下存放,手工切换,install安装python)
    http://blog.51cto.com/9940459/1868533


    pip常用命令
    列出安装的packages:pip freeze

    $ pip install --egg pyenv


    pip install pyenv

    NOTE: pip freeze will not show pyenv as installed as this tool is just a thin wrapper around the shell install script.


    step 安装pyenv
    https://github.com/pyenv/pyenv
    choose 左上角 clone or download

    ##wget https://github.com/yyuu/pyenv/archive/v20160726.tar.gz

    mv v20160726.tar.gz pyenv-20160726.tar.gz

    ##tar zxvf pyenv-20160726.tar.gz
    unzip pyenv-master*

    #######sed -i "s/https://www.Python.org/ftp/python/http://mirrors.sohu.com/pytn/g" `grep https://www.python.org/ftp/python -rl pyenv-20160726/plugins/python-build/share/python-build/*`

    sed -i "s/https://www.Python.org/ftp/python/http://mirrors.sohu.com/pytn/g" `grep https://www.python.org/ftp/python -rl pyenv-master/plugins/python-build/share/python-build/*`

    mv pyenv-master ~/.pyenv

    #######mv pyenv-20160726 ~/.pyenv

    export PATH=~/.pyenv/shims:~/.pyenv/bin:$PATH

    pyenv install -l

    vim ./bash_profile
    export PYENV_ROOT="$HOME/.pyenv"
    export PATH="$PYENV_ROOT/bin:$PATH"
    eval "$(pyenv init -)"


    卸载
    mv ~/.pyenv pyenv-20160726


    Uninstalling pyenv
    The simplicity of pyenv makes it easy to temporarily disable it, or uninstall from the system.

    To disable pyenv managing your Python versions, simply remove the pyenv init line from your shell startup configuration. This will remove pyenv shims directory from PATH, and future invocations like python will execute the system Python version, as before pyenv.
    pyenv will still be accessible on the command line, but your Python apps won't be affected by version switching.

    To completely uninstall pyenv, perform step (1) and then remove its root directory. This will delete all Python versions that were installed under $(pyenv root)/versions/ directory:

    rm -rf $(pyenv root)
    If you've installed pyenv using a package manager, as a final step perform the pyenv package removal. For instance, for Homebrew:

    brew uninstall pyenv


    #######
    命令:

    pyenv install -l 查看当前Pyenv支持的python版本。

    pyenv install 3.5.2 安装3.5.2版本

    pyenv local 3.5.2 切换本地目录下的python环境的版本号为3.5.2

    pyenv local system 切换本地目录下的Python环境的版本号为系统默认。

    pyenv virtualenv 3.5.2 linux 创建基于3.5.2 版本的虚拟环境

    pyenv uninstall linux 删除虚拟环境。

    #######

    如果下载太慢可以自已下载添加到 http://mirrors.sohu.com/python/ 下载源

    [root@client ~]# cd ~/.pyenv

    [root@client .pyenv]# cp /root/Python-2.7.12.tar.xz cache/

    [root@client .pyenv]# ls cache/

    Python-2.7.12.tar.xz

    [root@client .pyenv]# cd

    [root@client ~]# pyenv install 2.7.12

    **比如你创建 了 A 目录 并在里面pyenv local 2.7.12 (重要)

    创建 了 B目录。 并在里面pyenv local 3.5.2

    这样,你cd 进A目录,里面的python 就是2.7.12

    cd进B目录,里面的python 就是3.5.2的

    这样很方便的在各个版本之间切换。只要切换当前的目录就好

    [root@pythontest .pyenv]# pyenv install 2.7.14
    Installing Python-2.7.14...
    Installed Python-2.7.14 to /root/.pyenv/versions/2.7.14


    ##########pip 通过pip命令导出(python3)和导入Python环境安装包(python2)

    1.
    pip2 freeze > packages.txt

    2.
    python -m pip install -r packages.txt
    Downloading http://mirrors.aliyun.com/pypi/packages/44/48/def306413b25c3d01753603b1a222a011b8621aed27cd7f89cbc27e6b0f4/xlwt-1.3.0-py2.py3-none-any.whl (99kB)
    100% |████████████████████████████████| 102kB 3.8MB/s
    Installing collected packages: asn1crypto, pycparser, cffi, six, bcrypt, configparser, enum34, idna, ipaddress, cryptography, cx-Oracle, pytz, Django, django-crontab, django-guardian, mysqlclient, PyNaCl, pyasn1, paramiko, pyExcelerator, xlrd, xlwt, xlutils
    Running setup.py install for pycparser ... done
    Running setup.py install for configparser ... done
    Running setup.py install for django-crontab ... done
    Running setup.py install for mysqlclient ... done
    Running setup.py install for pyExcelerator ... done
    Successfully installed Django-1.11.16 PyNaCl-1.3.0 asn1crypto-0.24.0 bcrypt-3.1.4 cffi-1.11.5 configparser-3.5.0 cryptography-2.3.1 cx-Oracle-7.0.0 django-crontab-0.7.1 django-guardian-1.4.9 enum34-1.1.6 idna-2.7 ipaddress-1.0.22 mysqlclient-1.3.13 paramiko-2.4.2 pyExcelerator-0.6.4.1 pyasn1-0.4.4 pycparser-2.19 pytz-2018.7 six-1.11.0 xlrd-1.1.0 xlutils-2.0.0 xlwt-1.3.0
    You are using pip version 9.0.1, however version 18.1 is available.
    You should consider upgrading via the 'pip install --upgrade pip' command.
    [root@pythontest ~]#

    3.
    python -m pip download -r packages.txt


    4.
    pip install /root/package/six-1.11.0-py2.py3-none-any.whl

    or
    执行方法:
    1.使用setuptools工具安装xlrd模块
    1) 安装setuptools工具
    gunzip setuptools-2.0.tar.gz
    tar -xvf setuptools-2.0.tar
    python setup.py build
    python setup.py install

    2)安装xlrd模块
    gunzip xlrd-1.1.0.tar.gz
    tar -xvf xlrd-1.1.0.tar
    python setup.py install

    ######################3

    drwxr-xr-x. 4 root root 4096 12月 25 16:42 mysql_mon
    [root@pythontest db_monitor-master]# python manage.py runserver
    Performing system checks...

    Unhandled exception in thread started by <function wrapper at 0x7fe0a234ab90>
    Traceback (most recent call last):
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 124, in inner_run
    self.check(display_num_errors=True)
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/core/checks/model_checks.py", line 30, in check_all_models
    errors.extend(model.check(**kwargs))
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/db/models/base.py", line 1284, in check
    errors.extend(cls._check_fields(**kwargs))
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/db/models/base.py", line 1359, in _check_fields
    errors.extend(field.check(**kwargs))
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 913, in check
    errors = super(AutoField, self).check(**kwargs)
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 219, in check
    errors.extend(self._check_backend_specific_checks(**kwargs))
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 322, in _check_backend_specific_checks
    return connections[db].validation.check_field(self, **kwargs)
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/db/backends/mysql/validation.py", line 49, in check_field
    field_type = field.db_type(self.connection)
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 644, in db_type
    return connection.data_types[self.get_internal_type()] % data
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 174, in data_types
    if self.features.supports_microsecond_precision:
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/db/backends/mysql/features.py", line 53, in supports_microsecond_precision
    return self.connection.mysql_version >= (5, 6, 4) and Database.version_info >= (1, 2, 5)
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 385, in mysql_version
    with self.temporary_connection() as cursor:
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/db/backends/base/base.py", line 591, in temporary_connection
    cursor = self.cursor()
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/db/backends/base/base.py", line 254, in cursor
    return self._cursor()
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/db/backends/base/base.py", line 229, in _cursor
    self.ensure_connection()
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/db/backends/base/base.py", line 189, in connect
    self.connection = self.get_new_connection(conn_params)
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 274, in get_new_connection
    conn = Database.connect(**conn_params)
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/MySQLdb/__init__.py", line 85, in Connect
    return Connection(*args, **kwargs)
    File "/root/.pyenv/versions/2.7.14/lib/python2.7/site-packages/MySQLdb/connections.py", line 204, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
    django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")

    解决办法:


    https://blog.csdn.net/chenguolinblog/article/details/19421909

    vi 根目录:setting.py
    于是改成了以下的代码就ok了,实际上就是localhost的问题,只要替换为127.0.0.1即可

    然后新开一个窗口,重要
    重新运行python manage.py runserver

    #######

    [root@pythontest check_alarm]# python main_check.py
    2018-12-25 21:51:05,157 - main_check.py[line:793] - INFO: 清除osinfo表无效监控数据
    mysql execute: (2003, "Can't connect to MySQL server on '192.168.48.50' (110)")
    mysql execute: (2003, "Can't connect to MySQL server on '192.168.48.50' (110)")

    另外check_alarm/config/db_monitor.conf 也要mysql server 地址也要改


    ##########
    Traceback (most recent call last):
    File "main_check.py", line 881, in <module>
    alarm.alarm()
    File "/app/python/project/db_monitor-master/check_alarm/alarm.py", line 189, in alarm
    group by tags, host, port, service_name) t where cnt_all > %d ''' %(tags,100,num_max)
    UnboundLocalError: local variable 'tags' referenced before assignment


    解决方法:
    copy windows main_check.py to linux .

    ###############
    2018-12-25 22:18:40,537 - main_check.py[line:528] - ERROR: ora11g_test 数据库连接失败:DPI-1047: 64-bit Oracle Client library cannot be loaded: "libclntsh.so: cannot open shared object file: No such file or directory". See https://oracle.github.io/odpi/doc/installation.html#linux for help


    解决方法
    1.

    find / -name libclntsh.so
    /home/db/oracle/product/11.2.0/dbhome_1/lib/libclntsh.so


    2.
    export LD_LIBRARY_PATH=/home/db/oracle/product/11.2.0/dbhome_1/lib:$LD_LIBRARY_PATH
    export ORACLE_HOME=/home/db/oracle/product/11.2.0/dbhome_1


    ###########

    http://127.0.0.1:8000/admin

    添加用户和管理权限

    ###########1229

    #######如何传递vies函数的变量到模板
    https://code.ziqiangxuetang.com/django/django-template2.html


    view function -> function.name.html view 中指定返回模板名字
    在视图中(views)我们传递了一个字符串名称是 string 到模板 home.html,在模板中这样使用它:

    实例二,讲解了基本的 for 循环 和 List内容的显示

    views.py

    def home(request):
    TutorialList = ["HTML", "CSS", "jQuery", "Python", "Django"]
    return render(request, 'home.html', {'TutorialList': TutorialList})
    在视图中我们传递了一个List到模板 home.html,在模板中这样使用它:

    home.html

    教程列表:
    {% for i in TutorialList %}
    {{ i }}
    {% endfor %}


    2、model 中的queryset debug

    https://code.ziqiangxuetang.com/django/django-queryset-advance.html

    开始之前我们修改一个 settings.py 让Django打印出在数据库中执行的语句

    settings.py 尾部加上

    LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
    'console': {
    'class': 'logging.StreamHandler',
    },
    },
    'loggers': {
    'django.db.backends': {
    'handlers': ['console'],
    'level': 'DEBUG' if DEBUG else 'INFO',
    },
    },
    }

    python manage.py shell

    >>> from oracle_mon.models import TabOracleServers
    >> TabOracleServers.objects.all().order_by('tags')
    >> print str(TabOracleServers.objects.all().order_by('tags'))

    >>> for m in a:
    ... print (m.tags)
    ...

    QuerySet 是可迭代的,比如:

    es = Entry.objects.all()
    for e in es:
    print(e.headline)


    ############20181231

    1.
    Your models have changes that are not yet reflected in a migration, and so won't be applied.
    Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.


    python2 manage.py makemigrations
    python2 manage.py migrate


    check migration/db.log


    2.
    insert into check_list(check_tag,check_type,server_tag,begin_time,end_time) values(%s,%s,%s,%s,%s)"
    value = (file_tag,'Oracle数据库',','.join(select_tags),begin_time,end_time)


    mysql execute: (1364, "Field 'id' doesn't have a default value")

    solution:
    https://blog.csdn.net/u012110719/article/details/41850587
    AUTO_INCREMENT PRIMARY KEY,
    ALTER TABLE `check_list` CHANGE `id` `id` INT(11) NOT NULL AUTO_INCREMENT;

    3.
    mysql execute: (1364, "Field 'chk_time' doesn't have a default value")

    find the insert sql,insert add column and value
    insert into
    chk_time ->%s -> datetime.datetime.now()


    或者

    https://www.cnblogs.com/os-python/p/6773499.html
    想要解决问题就需要知道在mysql5.7中,启用了严格模式:
    在配置文件中 /etc/mysql/my.cnf 中找到:
    sql-model=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    修改为:sql-mode=NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    然后重启mysql,在重新同步模型到数据库

    4.
    URL ->view function -> function.name.html view 中指定返回模板名字


    url(r'^checklist_del/', frame.checklist_del),


    add views.py
    @login_required(login_url='/login')
    def checklist_del(request):
    check_tag = request.GET.get('check_tag')
    print check_tag
    models_frame.CheckList.objects.filter(check_tag=check_tag).delete()
    return HttpResponseRedirect('/my_check/')

    3.
    pip download --only-binary=:all: --platform linux_x86_64 --python-version 27 --implementation cp -d D: empzab pyenv

  • 相关阅读:
    想要快速上手 Spring Boot?看这些教程就足够了!| 码云周刊第 81 期
    Eclipse Java注释模板设置详解以及版权声明
    java跨域解决
    微信公众号创建
    Java与JS生成二维码与条形码
    Java基础break、continue语句的用法
    分布式文件系统介绍
    Hadoop的RPC机制及简单实现
    RPC简介与hdfs读过程与写过程简介
    JVM的Client模式与Server模式
  • 原文地址:https://www.cnblogs.com/feiyun8616/p/10355479.html
Copyright © 2020-2023  润新知