import socket target_host = "www.baidu.com" target_port = 80 # create a socket object client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # connect the client client.connect((target_host,target_port)) # send some data data = "GET / HTTP/1.1 Host: baidu.com " client.send(data.encode()) # receive some data response = client.recv(4096) print(response)
运行结果:
b'HTTP/1.1 200 OK
Date: Sun, 14 Feb 2016 03:34:20 GMT
Content-Type: text/html
Content-Length: 14613
Last-Modified: Wed, 03 Sep 2014 02:48:32 GMT
Connection: Keep-Alive
Vary: Accept-Encoding
Set-Cookie: BAIDUID=931D413C5
......