zoukankan      html  css  js  c++  java
  • python httplib get和post获取数据

    httplib 下的

    status http请求的状态  200 404 500...

    reason 返回答复 OK或者 FAULRE

    read()  读取内容

    get方法:

    #!/usr/bin/env python
    #coding=utf8
     
    import httplib
     
    httpClient = None
     
    try:
        httpClient = httplib.HTTPConnection('localhost', 80, timeout=30)
        httpClient.request('GET', '/test.php')
     
        #response是HTTPResponse对象
        response = httpClient.getresponse()
        print response.status
        print response.reason
        print response.read()
    except Exception, e:
        print e
    finally:
        if httpClient:
            httpClient.close()

    post获取数据:

    #!/usr/bin/env python
    #coding=utf8
     
    import httplib, urllib
     
    httpClient = None
    try:
        params = urllib.urlencode({'name': 'tom', 'age': 22})
        headers = {"Content-type": "application/x-www-form-urlencoded"
                        , "Accept": "text/plain"}
     
        httpClient = httplib.HTTPConnection("localhost", 80, timeout=30)
        httpClient.request("POST", "/test.php", params, headers)
     
        response = httpClient.getresponse()
        print response.status
        print response.reason
        print response.read()
        print response.getheaders() #获取头信息
    except Exception, e:
        print e
    finally:
        if httpClient:
            httpClient.close()
    

      

  • 相关阅读:
    数据库(2019年10月30日)
    (面试题)反射(2019年10月28日)
    反射(2019年10月28日)
    常微分复习重点
    重要定理及其证明
    实变函数复习重点
    泛函分析重点定理
    自旋玻璃简介
    Fnight博文发布规范
    [分析力学]解题思路
  • 原文地址:https://www.cnblogs.com/akidongzi/p/6222157.html
Copyright © 2011-2022 走看看