Instead of returning the values in an infinite loop from the server, I want to make a method, example getPositions() which returns the specific position that I want to while the connection server is still running. How do I do it?
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])

This should be pretty straight-forward:
EDIT: Created Method inside the get_coordinates function to re-establish socket connection in the event of a failure.