Created
July 5, 2013 18:03
-
-
Save atupal/5936190 to your computer and use it in GitHub Desktop.
python 长链接
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf-8 | |
''''' | |
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): | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment