# coding: utf-8
import socket
from urlparse import urlparse
def get_url(url):
# 通过socket请求html
url = urlparse(url)
host = url.netloc
path = url.path
if path == '':
path = '/'
# 建立socket连接
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((host, 80))
client.send('GET {} HTTP/1.1\r\nHOST:{}\r\nConnection:close\r\n\r\n'.format(path, host).encode('utf8'))
data = b''
while 1:
d = client.recv(1024)
if d:
data += d
else:
break
data = data.decode('utf8')
print data
client.close()
if __name__ == '__main__':
get_url('http://www.baidu.com')
网友评论