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)
  • 相关阅读:
    python---读取/写入excel用例数据
    unitest框架--认识与基本使用
    python--模拟蜂窝网(https)登陆总结
    python--实践--模拟浏览器(http)登陆
    python--return小练习
    python--smtp邮件使用
    关于商城价格变动对订单影响的问题
    history.back新页面跳转
    PHP无限极分类
    htaccess分布式配置文件常用写法
  • 原文地址:https://www.cnblogs.com/UnGeek/p/3293804.html
Copyright © 2011-2022 走看看