Python socket向百度發送http長連接請求 並做搜索
- #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