Chinaunix首页 | 论坛 | 博客
  • 博客访问: 222078
  • 博文数量: 88
  • 博客积分: 3020
  • 博客等级: 中校
  • 技术积分: 707
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-12 16:56
文章分类
文章存档

2010年(26)

2009年(62)

我的朋友

分类: Python/Ruby

2009-03-11 09:14:14

因为没有注意到格式,在http的头部没有添加content-type字段,所以导致服务器不能成功解析报文,加上之后就能顺利发帖了。data部分给出的是原始的数据,也可以用python代码实现,大家参考一下multipartposthandler的实现就明白了。
 
#!/usr/bin/python
import cookielib, urllib2, urllib, sys, time
cj = cookielib.CookieJar()
cookie = ""
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
exheaders = [("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; InfoPath.2; MAXTHON 2.0)"),]
opener.addheaders=exheaders
url_login = ''
body = (('forward',''), ('jumpurl',''),\
        ('step','2'),('lgt','0'),('pwuser','haha'),('pwpwd','hahaha'),('cktime','31536000'),\
        ('hideid','0'))
 

req1 = opener.open(url_login, urllib.urlencode(body))
t=req1.read()
for c in cj:
        cookie+=c.name+"="+c.value+";"
#cookie = "iscookies=0; f18cc_ol_offset=97; f18cc_threadlog=%2C2%2C; f18cc_readlog=%2C7%2C8%2C9%2C10%2C11%2C12%2C13%2C14%2C;" + cookie
print cookie
print(t.decode('gb2312'))
#sys.exit()
data = '-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="magicname"' + '\n' + '\n'+ '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="magicid"' + '\n' + '\n'+ '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="verify"' + '\n' + '\n' + \
'b7eb85be' + '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="atc_title"' + '\n'+ '\n' + \
'ertyry' + '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="atc_iconid"' + '\n' + '\n' + \
'0' + '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="atc_content"' + '\n' + '\n' + \
'yjuiyikuliu' + '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="atc_autourl"' + '\n' + '\n' + \
'1' + '\n' + \
'-----------------------------7d91d42da0af0'+ '\n'  + \
'Content-Disposition: form-data; name="atc_usesign"' + '\n' + '\n' + \
'1' + '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="atc_convert"' + '\n' + '\n' + \
'1' + '\n' + \
'-----------------------------7d91d42da0af0' + '\n'  + \
'Content-Disposition: form-data; name="atc_rvrc"' + '\n' + '\n' + \
'0' + '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="atc_enhidetype"' + '\n' + '\n' + \
'rvrc' + '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="atc_money"' + '\n' + '\n' + \
'0' + '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="atc_credittype"' + '\n' + '\n' + \
'money' + '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="atc_desc1"' + '\n' + '\n'+ '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="attachment_1"; filename=""' + '\n' + \
'Content-Type: application/octet-stream' + '\n' + '\n'+ '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="att_special1"' + '\n' + '\n' + \
'0' + '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="att_ctype1"' + '\n' + '\n' + \
'money' + '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="atc_needrvrc1"' + '\n' + '\n' + \
'0' + '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="step"' + '\n' + '\n' + \
'2' + '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="pid"' + '\n' + '\n'+ '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="action"' + '\n' + '\n' + \
'new' + '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="fid"' + '\n' + '\n' + \
'2' + '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="tid"' + '\n' + '\n'+ '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="article"' + '\n' + '\n' + \
'0' + '\n' + \
'-----------------------------7d91d42da0af0' + '\n' + \
'Content-Disposition: form-data; name="special"' + '\n' + '\n' + \
'0' + '\n' + \
'-----------------------------7d91d42da0af0--'+ '\n'

import httplib
 
http = httplib.HTTP('10.16.62.100')
# write header
http.putrequest("POST", '/phpwind/post.php?')
http.putheader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; InfoPath.2; MAXTHON 2.0)")
http.putheader("Referer", 'post.php?fid=2')
http.putheader("Host", '10.16.62.100')
http.putheader("Cookie", cookie)
http.putheader("Content-Type", 'multipart/form-data; boundary=---------------------------7d91d42da0af0')
http.putheader("Content-Length", str(len(data)))
http.endheaders()
 
# write body
http.send(data)
# get response
errcode, errmsg, headers = http.getreply()
if errcode != 200:
    raise Error(errcode, errmsg, headers)
file = http.getfile()
print file.read()
阅读(1647) | 评论(2) | 转发(0) |
0

上一篇:The httplib module

下一篇:访问远程注册表

给主人留下些什么吧!~~

yorks2009-07-22 12:49:30

已解决,read()要加入返回来的长度参数.

yorks2009-07-20 21:20:31

请问最后那个, 如果我在加入http.putheader("Connection", "Keep-Alive")加入后, file = http.getfile() print file.read() 这里出现timeout的错误,要如何修改呢?