V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
chinjanry
V2EX  ›  Python

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

  •  
  •   chinjanry · 2018-08-09 14:51:11 +08:00 · 3619 次点击
    这是一个创建于 2058 天前的主题,其中的信息可能已经有所发展或是发生改变。
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    #!/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 求高手指点。
    谢谢!
    3 条回复    2018-08-09 15:54:26 +08:00
    vimiix
        1
    vimiix  
       2018-08-09 15:41:38 +08:00
    为什么不用 `requests`
    jeremaihloo
        2
    jeremaihloo  
       2018-08-09 15:45:19 +08:00
    建议用 requests
    chinjanry
        3
    chinjanry  
    OP
       2018-08-09 15:54:26 +08:00
    找到原因了,是因为我开了网络代理。退出代理后就可以了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1825 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:28 · PVG 00:28 · LAX 09:28 · JFK 12:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.