• python发送post请求


     

     
    #!/usr/bin/python
    #-*-coding:utf-8-*-
      
    import httplib,urllib;  #加载模块
     
    #定义需要进行发送的数据
    params = urllib.urlencode({'title':'标题','content':'文章'});
    #定义一些文件头
    headers = {"Content-Type":"application/x-www-form-urlencoded",
               "Connection":"Keep-Alive","Referer":"http://mod.qlj.sh.cn/sing/post.php"};
    #与网站构建一个连接
    conn = httplib.HTTPConnection("http://mod.qlj.sh.cn/sing/");
    #开始进行数据提交   同时也可以使用get进行
    conn.request(method="POST",url="post.php",body=params,headers=headers);
    #返回处理后的数据
    response = conn.getresponse();
    #判断是否提交成功
    if response.status == 302:
        print "发布成功!";
    else:
        print "发布失败";
    #关闭连接
    conn.close();<span id="more-998"></span>

    不使用COOKIES 简单提交

    import urllib2, urllib
    &nbsp;
    data = {'name' : 'www', 'password' : '123456'}
    f = urllib2.urlopen(
            url     = 'http://www.ideawu.net/',
            data    = urllib.urlencode(data)
            )
    print f.read()

    使用COOKIES 复杂

    import urllib2&nbsp;
    cookies = urllib2.HTTPCookieProcessor()
    opener = urllib2.build_opener(cookies)
    &nbsp;
    f = opener.open('http://www.ideawu.net/?act=login&name=user01')
    &nbsp;
    data = '<root>Hello</root>'
    request = urllib2.Request(
            url     = 'http://www.ideawu.net/?act=send',
            headers = {'Content-Type' : 'text/xml'},
            data    = data)
    &nbsp;
    opener.open(request)
  • 相关阅读:
    【链表】Bzoj1098[POI2007]办公楼biu
    【构造】Bzoj1432[ZJOI2009]Function
    【Dp】Bzoj1296 [SCOI2009] 粉刷匠
    【二分贪心】Bzoj3969 [WF2013] Low Power
    【递推】Bzoj3612[Heoi2014]平衡
    HDU-3718 Similarity
    HDU-3435 A new Graph Game
    HDU-3488 Tour
    HDU-1853 Cyclic Tour
    HDU-1533 Going Home
  • 原文地址:https://www.cnblogs.com/UnGeek/p/3293804.html
Copyright © 2020-2023  润新知