我不想从服务器返回无限循环中的值,而是想创建一个方法,例如 getPositions(),它在连接服务器仍在运行时返回我想要的特定位置。我该怎么做?
import socket
import os,sys
import time
HOST = '59.191.193.59'
PORT = 5555
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((HOST,PORT))
screen_width = 0
screen_height = 0
while True:
client_socket.send("loc\n")
data = client_socket.recv(8192)
coordinates = data.split()
if(not(coordinates[-1] == "eom" and coordinates[0] == "start")):
continue
if (screen_width != int(coordinates[2])):
screen_width = int(coordinates[2])
screen_height = int(coordinates[3])
print int(coordinates[8])
print int(coordinates[9])
print int(coordinates[12])
print int(coordinates[13])