加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

httplib2 get,post实例

(2012-06-06 12:19:37)
标签:

it

分类: 编程经验--积跬步至千里
# -*- coding: UTF-8 -*-

def TestHttpGet():
    import httplib2

    #httplib2.debuglevel = 1

    word='中国'
    urlstr = 'http://fy.webxml.com.cn/webservices/EnglishChinese.asmx/TranslatorString' + '?wordKey=' + word

    h = httplib2.Http('.cache') 
    response,content = h.request(urlstr)

    #for item in response.items(): print(item)
    print(content.decode('utf-8'))
    #print(content)
    
def TestHttpPost():
  import httplib2
  from urllib.parse import urlencode    
  
  #httplib2.debuglevel = 1

  word='美国'
  urlstr = 'http://fy.webxml.com.cn/webservices/EnglishChinese.asmx/TranslatorString'

  data={'wordKey':word} 
  
  h = httplib2.Http('.cache')
  response,content = h.request(urlstr, 'POST', urlencode(data), headers={'Content-Type': 'application/x-www-form-urlencoded'})  

  #for item in response.items(): print(item)
  print(content.decode('utf-8'))
  #print(content)
  
TestHttpGet()
TestHttpPost()

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有