简体   繁体   English

使用 apache 和 mod_wsgi 部署 Django 站点后的套接字错误?

[英]Socket error after Django site deployed using apache and mod_wsgi?

There is a backend function in my site that will call urllib.urlopen(url)) to retrieve data from url.我的站点中有一个后端 function 将调用urllib.urlopen(url))从 url 检索数据。 After deploying, all other functions worked well except this one.部署后,除此之外的所有其他功能都运行良好。 Calling this function results in [Errno socket error] [Errno -2] Name or service not known .调用此 function 会导致[Errno socket error] [Errno -2] Name or service not known It seems that it can't find the host.好像找不到主机了。

But if I use python manage.py runserver to run the site, this function works well.但是如果我使用python manage.py runserver来运行站点,这个 function 运行良好。

I'm wondering whether maybe there is some problem with Apache, but if there is I can't find it.我想知道Apache是否有问题,但如果有我找不到。 Thanks for your help.谢谢你的帮助。

This is the function:这是 function:

WORD_URL = 'http://dict.cn/ws.php?utf8=true&q=%s'

def get_word(word):
    url = WORD_URL % word
    dom = minidom.parse(urllib.urlopen(url))
    try:
        pron = dom.getElementsByTagName('pron')[0].firstChild.data
        definition = dom.getElementsByTagName('def')[0].firstChild.data
    except IndexError:
        pron = ''
        definition = ''
    return {
        'word':word,
        'pron':pron,
        'definition':definition
        }

This is the traceback:这是回溯:

Traceback:
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/jxq/djcode/wormo/core/views.py" in added
31.             xml_word = get_word(new_word)
File "/home/jxq/djcode/wormo/core/get_word.py" in get_word
8.     dom = minidom.parse(urllib.urlopen(url))
File "/usr/lib/python2.7/urllib.py" in urlopen
84.         return opener.open(url)
File "/usr/lib/python2.7/urllib.py" in open
205.                 return getattr(self, name)(url)
File "/usr/lib/python2.7/urllib.py" in open_http
342.         h.endheaders(data)
File "/usr/lib/python2.7/httplib.py" in endheaders
937.         self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py" in _send_output
797.         self.send(msg)
File "/usr/lib/python2.7/httplib.py" in send
759.                 self.connect()
File "/usr/lib/python2.7/httplib.py" in connect
740.                                              self.timeout, self.source_address)
File "/usr/lib/python2.7/socket.py" in create_connection
553.     for res in getaddrinfo(host, port, 0, SOCK_STREAM):

Exception Type: IOError at /wormo/added/
Exception Value: [Errno socket error] [Errno -2] Name or service not known

httpd.conf: httpd.conf:

WSGIScriptAlias / /home/jxq/djcode/wormo/apache/django.wsgi

<Directory /home/jxq/djcode/wormo/apache>
    Order allow,deny
    Allow from all
</Directory>

Alias /media/ /home/jxq/djcode/wormo/media/

<Directory /home/jxq/djcode/wormo>
Order deny,allow
Allow from all
</Directory>

Python 3 and Python 2.7 are both on my machine. Python 3 和 Python 2.7 都在我的机器上。 Is this a problem?这是一个问题吗?

It's possible that this could be a permission issue or something else with your Apache environment.这可能是您的 Apache 环境的权限问题或其他问题。 Try using a simple WSGI script as a baseline to test URL fetching:尝试使用一个简单的 WSGI 脚本作为基线来测试 URL 获取:

import sys
import urllib    

def application(environ, start_response):
   page_text = urllib.urlopen("http://www.google.com/").read()
   start_response('200 OK', [
     ('Content-Type', 'text/html'),
     ('Content-Length', str(len(page_text))),
   ])
   yield page_text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM