一个Web服务器及python作web开发的框架:Tornado 托内科及python提示报错:ImportError: No module named OpenSSL的一路安装解决

一、一个Web服务器及python作web开发的框架:Tornado 托内科

    tornado,是使用Python编写的一个强大的、可扩展的Web服务器及Python作web开发框架。网上说Tornado和现在的主流Web服务器框架(包括大多数 Python 的框架)有着明显的区别:它是非阻塞式服务器,而且速度相当快。得利于其非阻塞的方式和对epoll的运用,Tornado每秒可以处理数以千计的连接,因此Tornado是实时 Web 服务的一个理想框架。这些我目前还没了解那么多,先不多说。听这些介绍好像不错。

    简单地说,或者用php来说,tornado就是你的nginx+thinkPHP(随便一个框架)的组合体。把tornado搭建好了之后,直接启动tornado下的python脚本命令(指定端口),就可以实现以WEB的形式访问服务器,tornado端可以查看访问的日志。类似于nginx的请求日志。

1. tornado的安装:

我的服务器已经搭建好了pip,安装起来非常简单。直接pip install tornado即完工。

2. tornado第一段python代码:hello.py

#!/usr/bin/python
#coding:utf-8

#引入必要库
import tornado.httpserver;
import tornado.ioloop;
import tornado.options;
import tornado.web;

from tornado.options import define,options;
define("port", default=8080, help="run on the given port", type=int)

#定义一个处理的方法,也就是一个控制器
class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        who = self.get_argument('who', '')
        if who == '':
            self.write('please pass parameter:who through get');
        else:
            self.write('hello ' + who + ', how are you!'+"\n")

#框架启动时的执行就去,绑定执行控制器和端口,开始监听
if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application(handlers=[("/", IndexHandler)])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

3. 启动tornado WEB服务器并访问

        启动python脚本,则开始监听端口,但启动时出现报错:NameError: global name 'memoryview' is not defined

#报错如下:
[E 180110 14:42:19 http1connection:731] Uncaught exception
    Traceback (most recent call last):
      File "/usr/lib64/python2.6/site-packages/tornado/http1connection.py", line 722, in _server_request_loop
      ......
      File "/usr/lib64/python2.6/site-packages/tornado/iostream.py", line 910, in _consume
        b = (memoryview(self._read_buffer)
    NameError: global name 'memoryview' is not defined

         这个报错是版本不兼容,可以降低tornado版本,也可以提高python版本,我的python版本是2.6,我这里降低了tornado版本。如下:

#卸载tornado
pip uninstall tornado
#更新安装tornado版本
pip install tornado==2.1.1

         再次启动python脚本,并从浏览器里请求。命令启动后实时显示的请求日志如下:如下图,图中的--port实际也可以省略,因为程序中已经设置了默认的port8080

        浏览器中的请求截图:

二、python提示报错:ImportError: No module named OpenSSL的一路安装解决

         在调试一个很大的python程序时出现报错,ImportError: No module named OpenSSL,找到报错的文件位置就一行代码,于是把它写到 ~/a.py中脚本如下:

#!/usr/bin/python
#coding:utf-8

import OpenSSL
print 'yes this is python'

 开始一路安装各种东西来解决它:

1,首先报错ImportError: No module named OpenSSL是缺少OpenSSL模块,google找到办法:

You can install the relevant package using the following command:
#sudo yum install python-openssl
#The other option would be to install the OpenSSL paackage using Python's pip:
#sudo pip install pyOpenSSL

2,报错:ImportError: No module named cryptography

执行以上命令。安装完毕,再执行python a.py
但接下来又提示新报错:ImportError: No module named cryptography
了解Pycrypto是一个解密加密的工具,估计也是pyOpenSSL里用到的加密解密工具。

#yum里也安装,这个命令持保留意见
sudo yum install pycrypto
#python 安装,好像必须先执行pycrypto安装,
pip install pycrypto
pip install cryptography

 3,报错No local packages or download links found for cffi>=1.7

        但在安装cryptography时发现又提示新的报错:No local packages or download links found for cffi>=1.7。

于是先安装cffi:pip install cffi
再执行安装pip install cryptography 然后cryptography此步安装成功
Successfully installed cryptography idna asn1crypto
Cleaning up...

4,报错:ImportError: No module named enum。

        接下来再执行python a.py 又出现报错:ImportError: No module named enum再安装:pip install enum

5,报错:ImportError: No module named ipaddress

        以为这下差不多了,但再执行时又有新的报错花样:ImportError: No module named ipaddress
再安装:pip install ipaddress
Successfully installed ipaddress
Cleaning up...

6,这回来了个难搞的报错:TypeError: 'type' object is not iterable

执行时的报错详细如下:
_ASN1_TYPE_TO_ENUM = dict((i.value, i) for i in _ASN1Type)
TypeError: 'type' object is not iterable
找来找去,最后google到解决方法:

执行上面的命令:

pip uninstall enum
pip install enum34

终于得到解决:python脚本执行结果如下:

[root@123 ~]# cat a.py 
#!/usr/bin/python
#coding:utf-8

import OpenSSL
print 'yes this is python'
[root@123 ~]# python a.py
/usr/lib64/python2.6/site-packages/cryptography/__init__.py:26: DeprecationWarning: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. The next version of cryptography will drop support for Python 2.6
  DeprecationWarning
yes this is python

 7,关闭一些不要的提示:

        上面执行中发现有一个过期提醒,The next version of cryptography will drop support for Python 2.6 。可以不让它显示。

[root@123 ~]# python -W ignore a.py
yes this is python
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林戈的IT生涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值