服务端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import socket


class Server(object):
def __init__(self):
# 创建 监听服务
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

def start(self, port=8000):
# 绑定端口
self.server.bind(("", port))
# 监听等待客户端连接数
self.server.listen(128)

# 循环监听
while True:
# 获取客户端连接数据
client, addr = self.server.accept()

# 解析客户端数据
request_data = client.recv(1024)
print request_data
method = str(request_data).split("\r\n")[0].split("/")[0].replace(" ", "")

# 拼接 HTTP 返回头
response_start_line = "HTTP/1.1 200 OK\r\n"
response_head_line = "Server: My server\r\n"

response = ""
if method == "GET":
response_body = "<h1>Hello World GET!</h1>"
response = response_start_line + response_head_line + "\r\n" + response_body
elif method == "POST":
response_body = "<h1>Hello World POST !</h1>"
response = response_start_line + response_head_line + "\r\n" + response_body

# 发送消息到客户端
client.send(response)

# 关闭连接
client.close()


if __name__ == '__main__':
Server().start(port=8080)

客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import socket


class ReqUrl(object):
'''
创建 GET POST 基础类
'''

def req(self, host, port, header):
'''
:title Socket 请求
:param host: url 地址 主机名
:param port: 端口
:param header: 请求头
:return: 返回请求结果
'''
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((host, port))
client.send(header)
response = client.recv(9999)

result = ""
for r in str(response).split("\r\n")[3:]:
result = result + r
return result

def get_host_port(self, url):
'''
:title 解析URL
:param url: 请求的URL
:return: 解析的结果
'''
host = url.split("/")[2]

url_header = ""
for a in url.split("/")[3:]:
url_header = url_header + "/" + a

if url_header is "":
url_header = "/"

return host, url_header

def req_get(self, url, port=80):
'''
:title GET 请求
:param url:请求的URL
:param port:端口
:return: 返回的结果
'''

# GET 请求头
get_header = 'GET {url_header} HTTP/1.1\r\n' \
'Host:{url_host} \r\n' \
'User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36\r\n' \
'Accept: */*\r\n\r\n'

# 获取主机名 和 请求头URL路径
host, url_header = self.get_host_port(url=url)

# 开始请求
data = self.req(host=host, port=port, header=get_header.format(url_header=url_header, url_host=host))

# 返回结果
return data

def req_post(self, url, data=None, port=80):
'''
:title POST 请求
:param url:请求的URL
:param data:请求数据
:param port:端口
:return:返回请求结果
'''

# POST 请求头
post_header = 'POST {url_header} HTTP/1.1\r\n' \
'Host:{url_host} \r\n' \
'User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36\r\n' \
'Accept: */*\r\n\r\n'

# 获取主机名 和 请求头URL路径
host, url_header = self.get_host_port(url=url)

# 开始请求
data = self.req(host=host, port=port, header=post_header.format(url_header=url_header, url_host=host))

# 返回结果
return data


class Req(object):
'''
创建这个为了简化引用
'''

@staticmethod
def get(url, port=80):
return ReqUrl().req_get(url=url, port=port)

@staticmethod
def post(url, data=None, port=80):
return ReqUrl().req_post(url=url, data=data, port=port)


if __name__ == '__main__':
# GET 请求
r = Req.get(url="http://localhost/", port=8080)
print r

# POST 请求
r = Req.post(url="http://localhost/", port=8080)
print r

运行

保存代码为 .py 文件,运行服务端,默认端口 8080。
然后运行客户端请求即可,默认请求端口默认 8080。
自己根据实际情况修改