【问题标题】:Python 2.7: urllib2.urlopen(request) not working with DjangoPython 2.7:urllib2.urlopen(request) 不适用于 Django
【发布时间】:2019-05-15 02:38:53
【问题描述】:

如果我使用 Django,我无法使用 urllib2 发出请求,但是,单独运行脚本可以。

如果我在与 Django 相同的虚拟环境中单独运行脚本,它可以工作。

我正在使用的代码:

data = urllib.urlencode({'somedata':somedata})
url = "https://www.example.com"
request = urllib2.Request(url,data)
response = urllib2.urlopen(request).read()
print response

Django尝试运行时出现问题,我尝试切换网络和操作系统:

在 Ubuntu 中,我收到此错误:

<urlopen error [Errno 113] No route to host>

在 Windows 中:

<urlopen error [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>

知道为什么会这样吗?

【问题讨论】:

    标签: python django python-2.7 urllib2


    【解决方案1】:

    确保环境有dns服务器,网络连接可达

    【讨论】:

      猜你喜欢
      • 2012-08-18
      • 2012-05-25
      • 2012-04-11
      • 2012-01-13
      • 1970-01-01
      • 2017-02-22
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多