TCP编程时,recv()函数默认阻塞等待,返回socket携带的信息,要根据业务场景设计recv的等待和结束机制:
import socket
address = ("0.0.0.0", 12346)
def tcp_server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(address)
server_socket.listen(5)
print("TcpServer开始监听:{}".format(address))
while True:
client_socket, client_address = server_socket.accept()
data = str()
while True:
msg = client_socket.recv(1024)
data += msg
if not len(msg):
break
print(client_address, ":", data.decode())
client_socket.close()
if __name__ == '__main__':
tcp_server()
import socket
import threading
import subprocess
class TcpServer:
def __init__(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("0.0.0.0", 60000))
s.listen(5)
print("Waiting for connection...")
while True:
# 接收一个新连接
sock, address = s.accept()
# 创建新的线程来处理tcp连接
t = threading.Thread(target=self.tcp_link, args=(sock, address), daemon=True)
t.start()
def tcp_link(self, sock, address):
print("Accept new connection from %s:%s..." % address, "="*30)
terminator = "abcdefg"
buffer = str()
while True:
data = sock.recv(1024).decode()
buffer += data
if buffer[-7:] == terminator:
break
buffer = buffer[:-7]
print("accept data:{}".format(buffer))
if buffer[-4:] == ".bat":
# 后缀为.bat,说明要运行批处理文件
print(".bat file running")
self.run_bat(buffer)
else:
# 其他的判定为运行adb命令
print("adb command running")
command_reply = self.run_command(buffer) + terminator
reply = str(command_reply).encode()
sock.send(reply)
sock.close()
print("Connection from %s:%s closed!" % address, "="*30)
@staticmethod
def run_bat(bat_file_path):
subprocess.getoutput(str(bat_file_path))
try:
subprocess.check_output(str(bat_file_path),
shell=True,
universal_newlines=True,
stderr=subprocess.STDOUT,
timeout=3)
except subprocess.CalledProcessError as ex:
print('run .bat file error, message: {}'.format(ex.output))
except subprocess.TimeoutExpired:
print('run .bat file error, because of timeout')
@staticmethod
def run_command(command):
try:
# 超时为3秒
s = subprocess.check_output(command,
shell=True,
universal_newlines=True,
stderr=subprocess.STDOUT,
timeout=3)
except subprocess.CalledProcessError as ex:
s = ex.output
except subprocess.TimeoutExpired:
s = "TimeOut"
return s
if __name__ == '__main__':
print("TCP server is start !")
TcpServer()
import socket
import time
address = ("0.0.0.0", 12345)
def tcp_server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(address)
server_socket.listen(5)
print("TcpServer开始监听:{}".format(address))
while True:
client_socket, client_address = server_socket.accept()
client_socket.setblocking(0)
time.sleep(2)
try:
msg = client_socket.recv(1024).decode()
except BlockingIOError as e:
msg = e
print(client_address, ":", msg)
client_socket.close()
if __name__ == '__main__':
tcp_server()