- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#!/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 求高手指点。
谢谢!