#!/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 = 'Hello'
request = urllib2.Request(
url = 'http://www.ideawu.net/?act=send',
headers = {'Content-Type' : 'text/xml'},
data = data)
opener.open(request)