推荐学习书目
› 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 · Aug 9, 2018 · 4794 views
    This topic created in 2979 days ago, the information mentioned may be changed or developed.
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    #!/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 replies  •  2018-08-09 15:54:26 +08:00
    vimiix
        1
    vimiix  
       Aug 9, 2018
    为什么不用 `requests`
    jeremaihloo
        2
    jeremaihloo  
       Aug 9, 2018
    建议用 requests
    chinjanry
        3
    chinjanry  
    OP
       Aug 9, 2018
    找到原因了,是因为我开了网络代理。退出代理后就可以了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   752 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:50 · PVG 05:50 · LAX 14:50 · JFK 17:50
    ♥ Do have faith in what you're doing.