Python网络编程 - 一个简单的客户端Get请求程序

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
......

上一篇:JavaScript学习笔记——js变量的布尔值


下一篇:使用COCOS2D-X JSB开发,在js页面中设置iOS键盘模式