-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceive.py
More file actions
27 lines (23 loc) · 774 Bytes
/
receive.py
File metadata and controls
27 lines (23 loc) · 774 Bytes
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
#!/usr/bin/env python
# coding=utf-8
import socket
import json
ListenSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ListenSocket.bind(('127.0.0.1', 5701))
ListenSocket.listen(100)
HttpResponseHeader = '''HTTP/1.1 200 OK\r\n
Content-Type: text/html\r\n\r\n
'''
def request_to_json(msg):
for i in range(len(msg)):
if msg[i]=="{" and msg[-1]=="\n":
return json.loads(msg[i:])
return None
#需要循环执行,返回值为json格式
def rev_msg():# json or None
Client, Address = ListenSocket.accept()
Request = Client.recv(1024).decode(encoding='utf-8')
rev_json=request_to_json(Request)
Client.sendall((HttpResponseHeader).encode(encoding='utf-8'))
Client.close()
return rev_json