zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    Java程序员必会的工具库,代码量减少90%
    Git常用操作
    Eclipse开发环境配置
    Spring Cloud Alibaba Nacos 在Windows10下启动
    MySQL连接异常Communications link failure
    Spring Cloud Alibaba Nacos2.0踩坑
    Spring Cloud Alibaba Nacos配置中心与服务发现
    RocketMQ学习笔记
    Linux开发环境配置
    Clumper尝鲜
  • 原文地址:https://www.cnblogs.com/UnGeek/p/3293804.html
Copyright © 2011-2022 走看看