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\r\nHost: baidu.com\r\n\r\n" client.send(data.encode()) # receive some data response = client.recv(4096) print(response)
运行结果:
b'HTTP/1.1 200 OK\r\nDate: Sun, 14 Feb 2016 03:34:20 GMT\r\nContent-Type: text/html\r\nContent-Length: 14613\r\nLast-Modified: Wed, 03 Sep 2014 02:48:32 GMT\r\nConnection: Keep-Alive\r\nVary: Accept-Encoding\r\nSet-Cookie: BAIDUID=931D413C5
......