使用 apache 和 mod_wsgi 部署 Django 站点后出现套接字错误?
我的网站中有一个后端函数,它将调用 urllib.urlopen(url)) 来从 url 检索数据。部署后,除了这个之外,其他功能都运行良好。调用此函数会导致[Errno套接字错误] [Errno -2]名称或服务未知
。看来是找不到主人了。
但如果我使用 python manage.py runserver 来运行该网站,这个功能就可以正常工作。
我想知道 Apache 是否有问题,但如果有的话我找不到它。感谢您的帮助。
这是函数:
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
}
这是回溯:
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:
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 和 Python 2.7 都在我的机器上。这是一个问题吗?
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_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:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是您的 Apache 环境的权限问题或其他问题。尝试使用简单的 WSGI 脚本作为基准来测试 URL 获取:
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: