【转】 python socket向百度发送http长连接请求 并做搜索
2011-10-08 22:13
#coding=gbk ''' socket 给百度发送http请求 连接成功后,发送http的get请求,所搜索功能 ''' import socket import sys import time if __name__=='__main__': #创建套接字 try : sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.eorror,e: print 'socket false:%s'%e print 'socket ...' #连接百度ip try : sock.connect(('220.181.111.148',80)) except socket.error,e: print 'connect false %s'%e sock.close() print 'connect ...' #发送百度首页面请求并且保持连接 try : print 'send start...' str='GET / HTTP/1.1\r\nHost:www.baidu.com\r\nConnection:keep-alive\r\n\r\n' sock.send(str) except socket.eorror,e: print 'send false' sock.close() data='' data = sock.recv(1024) while (1): '''如何判断数据接收完毕,在发送http 最前端, 包含发送数据文件大小属性Content-Length, 用字符匹配方式取得文件大小, 同过大小判断是否接收完毕。 ''' print data beg = data.find('Content-Length:',0,len(data)) end = data.find('Content-Type:',0,len(data)) print beg print end if(beg == end): print 'connecting closed' break num = long(data[beg+16:end-2]) print num nums = 0 while (1): data=sock.recv(1024) print data nums +=len(data) if(nums >= num): break word = raw_input('please input your word----->') str='''GET /s?wd=''' + word + ''' HTTP/1.1 Host:www.baidu.com Connection: Keep-Alive ''' print str sock.send(str) data = '' data = sock.recv(1024) sock.close() print data |
| +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 用python搜索google from:http://ipython.iteye.com/blog/336304
如果直接用urllib.urlopen(url)会返回一个forbidden的,其实加一个add_header就可以了. 给出一个关键字 word 就会返回搜索结果了。
import urllib import urllib2 def search(word): url=r'http://www.google.cn/search?hl=zh-CN&newwindow=1&q='+word+"&start=10&sa=N" req=urllib2.Request(url) req.add_header("User-Agent",'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322)') opener=urllib2.build_opener() text=opener.open(req).read() return text
python通过socket查询whois脚本
from:http://www.oschina.net/code/snippet_54100_2870
import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("whois.networksolutions.com", 43)) s.send("sina.com.cn \r\n") while 1: v = s.recv(1024) if v == "" or v == None: break print v s.close()
| |
|