谷歌測試代碼
import socket import time import re import os import subprocess PC_State = True IP_Adress_Str = "" IP_PortNum = 0; host = '' port = 11111 bufsize = 1024 addr = (host, port) tcpServer = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) tcpServer.bind(addr) s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) while True: IsComing = False Str = "" if (PC_State == True): print("Waiting for receiving...") Str,tcpClient = tcpServer.recvfrom(bufsize) Str = str(Str,encoding="utf-8") if "&&" in Str: IsComing = True print("Receive Signal from: ", tcpClient) Pre_IP = Str[Str.find("//") + 2: Str.find("//") + 2+Str.find("&&") - Str.find("//") - 2]; mode = Str[Str.find("&&") + 2:Str.find("&&") + 2+len(Str) - Str.find("&&") - 2]; Str = Str[0:Str.find("//")] # Server mode: if "Server" in mode: print("Start Working in Server mode ....") cmd = subprocess.Popen(Str); #server_str=cmd.communicate() # if "listening on" not in server_str: # cmd.kill() # continue print("Send PC_Over Signal ....") tcpServer.sendto(bytes("PC_Over", encoding="utf-8"),(tcpClient[0],port)) print("Start Receiving Over Signal ....") readvalue, tcpClient = tcpServer.recvfrom(bufsize) readvalue = str(readvalue, encoding="utf-8") if "Over" in readvalue: cmd.kill() continue elif "Client" in mode: tcpClient.send(bytes(('OK to client'), encoding="utf-8")) cmd = subprocess.Popen(Str); server_str = cmd.communicate() readvalue = tcpClient.recv(bufsize).decode(encoding="utf-8") if "Over" in readvalue: cmd.kill() continue