开发者

Socket error after Django site deployed using apache and mod_wsgi?

开发者 https://www.devze.com 2023-03-09 14:59 出处:网络
There is a backend function in my site that will call urllib.urlopen(url)) to retrieve data from url. After deploying, all other functions worked well except this one. Calling this function results in

There is a backend function in my site that will call urllib.urlopen(url)) to retrieve data from 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. 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.

I'm wondering whether maybe there is some problem with Apache, but if there is I can't find it. Thanks for your help.

This is the 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_htt开发者_运维问答p
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:

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. Is this a problem?


It's possible that this could be a permission issue or something else with your Apache environment. Try using a simple WSGI script as a baseline to test URL fetching:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消