Python urllib2 urlopen 请求 url,一直卡住,求解。

2018-08-09 14:51:11 +08:00
 chinjanry
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#!/usr/bin/env python
#coding=utf-8
import urllib, urllib2, sys, ssl, cgi, json

print("Content-type:text/json; charset=UTF-8\r\n\r\n")

url = 'https://aip.baidubce.com/...'

request = urllib2.Request(url)

#添加请求头
request.add_header('Content-Type', 'application/json; charset=UTF-8')

response = urllib2.urlopen(request)

content = response.read()

if (content):
print(content)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

以上代码,执行 urlopen 处时,只要 url 是外部的,都连不上。不管改成哪个网站,不管使用 http 还是 https 都没什么卵用,但我改成 http://localhost/index.html 竟然没问题。百思不得其解。我环境: MacOS、Apache2 求高手指点。
谢谢!
3659 次点击
所在节点    Python
3 条回复
vimiix
2018-08-09 15:41:38 +08:00
为什么不用 `requests`
jeremaihloo
2018-08-09 15:45:19 +08:00
建议用 requests
chinjanry
2018-08-09 15:54:26 +08:00
找到原因了,是因为我开了网络代理。退出代理后就可以了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/478276

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX