WSGI与python3无法正常工作

时间:2015-07-13 15:30:44

标签: python beautifulsoup urllib wsgi

我正在尝试在网上运行python来从网站上进行一些CSS / JSS提取。我使用mod_wsgi作为python的接口。我一直在关注this网站,以了解入门的想法。 以下是他们的示例代码。

#! /usr/bin/env python

# Our tutorial's WSGI server
from wsgiref.simple_server import make_server

def application(environ, start_response):

   # Sorting and stringifying the environment key, value pairs
   response_body = ['%s: %s' % (key, value)
                    for key, value in sorted(environ.items())]
   response_body = '\n'.join(response_body)

   status = '200 OK'
   response_headers = [('Content-Type', 'text/plain'),
                  ('Content-Length', str(len(response_body)))]
   start_response(status, response_headers)

   return [response_body]

# Instantiate the WSGI server.
# It will receive the request, pass it to the application
# and send the application's response to the client
httpd = make_server(
   'localhost', # The host name.
   8051, # A port number where to wait for the request.
   application # Our application object name, in this case a function.
   )

# Wait for a single request, serve it and quit.
httpd.handle_request()

虽然使用python 2.7运行良好,但我不能让它在Python 3上运行。对于我的CSS / JSS提取,我修改了上面的代码并放入了我自己的功能,使用BeautifulSoup和urllib3。虽然使用这些模块我需要python 3,但对于WSGI代码我需要python 2.7。因此,我无法合并这两者。当试图在python3中运行BS和urllib时,我收到一个错误。但是当我尝试使用python3运行WSGI代码时,我只是无法加载网页。

任何帮助将不胜感激!任何变通办法或建议。

0 个答案:

没有答案