码迷,mamicode.com
首页 > 编程语言 > 详细

Python 3.x - 一个简单的客户端Get请求程序

时间:2016-02-14 12:58:16      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

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

 

Python 3.x - 一个简单的客户端Get请求程序

标签:

原文地址:http://www.cnblogs.com/davidgu/p/5188870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!