Python的Get和Post提交-我在杭电第四届网络攻防大赛写python脚本

有道题叫“鸟叔来了”,感觉应该是用python脚本写写,然后跑跑就行啦,但是失败了,不过,这次失败,让我对python脚本有了一个新的认知。我会慢慢改进的。。
import urllib
import httplib
import time
from re import search
for i in range(140,151):
	url='http://lab.sh1n3.cn/howmany.php'

	#POST	
	headers = {'Host': 'lab.sh1n3.cn',
		   'Connection': 'keep-alive',
		   'Content-Length': '24',
		   'Cache-Control': 'max-age=0',
		   'Origin': 'http://lab.sh1n3.cn',
		   'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) 
Chrome/19.0.1084.52 Safari/536.5',
		   'Content-Type': 'application/x-www-form-urlencoded',
		   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                   'Referer': 'http://lab.sh1n3.cn/howmany.php',
                   'Accept-Encoding': 'gzip,deflate,sdch',
                   'Accept-Language': 'zh-CN,zh;q=0.8',
                   'Accept-Charset': 'GBK,utf-8;q=0.7,*;q=0.3',
                   'Cookie': 'cookie_mark=1351955165'}  
	
	postData = {'answer' : i,'submit':'Submit'}    
	postData = urllib.urlencode(postData)
	
	conn = httplib.HTTPConnection('lab.sh1n3.cn', 80)  
	conn.request('POST','/howmany.php',postData,headers)
	res = conn.getresponse()
	data = res.read()
	print res.reason
	print i
	if search('How many developers?',data):
		print 'normal'
	if search('Fast',data):
		print 'Fast'
	if search('key',data):
		print 'key'
		print data
		break
	
	'''
	#Get
	getData=[('answer',i),('submit','Submit')]
	url = url + "?" + urllib.urlencode(getData)
	x=urllib.urlopen(url)
	y=x.read()
	x.close()
	print i
	print url
	print y
	 
	if len(y)!=431:
		print 'ok'
		exit(0)
	if search('How many developers?',y):
		print 'normal'
	if search('Fast',y):
		print 'Fast'
	if search('key',y)
		print 'key'
		print data
	'''
	time.sleep(1)



你可能感兴趣的:(Python的Get和Post提交-我在杭电第四届网络攻防大赛写python脚本)