python http post数据表单

date:星期二, 三月 30th, 2010 at 2:56 上午 Categories:python

学习PYTHON

#!/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();

不使用COOKIES 简单提交

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

使用COOKIES 复杂

import urllib2
 
cookies = urllib2.HTTPCookieProcessor()
opener = urllib2.build_opener(cookies)
 
f = opener.open('http://www.ideawu.net/?act=login&name=user01')
 
data = '<root>Hello</root>'
request = urllib2.Request(
        url     = 'http://www.ideawu.net/?act=send',
        headers = {'Content-Type' : 'text/xml'},
        data    = data)
 
opener.open(request)

5 Responses to “python http post数据表单”

  1. rookNo Gravatar

    #注意,使用的是python3.1版本
    import http.client, urllib.parse
    #注意下面的语句,如果直接写成json,python以为是dict数据
    values=’{‘name’ : ‘www’, ‘password’ : ‘123456′}’;

    headers = {“Content-type”: “application/x-www-form-urlencoded”,
    “Accept”: “text/plain”}
    conn = http.client.HTTPConnection(“http://mod.qlj.sh.cn/sing/”)
    #直接对values进行encode就可以了
    conn.request(“POST”, “/”, values.encode() , headers)
    response = conn.getresponse()
    #输出返回码
    print(response.status, response.reason)

    data = response.read()
    #输出返回数据
    print(data);
    conn.close()

    [Reply]

  2. Shepherdsville KY

    Shepherdsville KY…

    [...]python http post数据表单[...]…

  3. cheap pearl jewelry,cheap body jewelry, free shipping

    cheap pearl jewelry,cheap body jewelry, free shipping…

    [...]python http post数据表单[...]…

  4. cheap rhinestone jewelry,wholesale cheap jewelry

    cheap rhinestone jewelry,wholesale cheap jewelry…

    [...]python http post数据表单[...]…

  5. iso 9001 ,norma iso 9001,normas iso 9001 ,iso 9001,iso9001,calidad iso ,sistemas iso ,gestion iso ,manual iso ,auditoria iso ,certificacion iso ,certificaciones iso ,implementar iso ,implementacion iso

    iso 9001 ,norma iso 9001,normas iso 9001 ,iso 9001,iso9001,calidad iso ,sistemas iso ,gestion iso ,manual iso ,auditoria iso ,certificacion iso ,certificaciones iso ,implementar iso ,implementacion iso…

    [...]python http post数据表单[...]…

Leave a Reply