简体   繁体   中英

how do i make this while loop into a method?

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:

import socket
import os,sys
import time
from threading import Thread

HOST = '59.191.193.59'
PORT = 5555

COORDINATES = []

def connect():   
    globals()['client_socket'] = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((HOST,PORT))

def update_coordinates():
    connect()
    screen_width = 0
    screen_height = 0
    while True:
        try:
            client_socket.send("loc\n")
            data = client_socket.recv(8192)
        exept:
            connect();
            continue;

        globals()['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])            

Thread(target=update_coordinates).start()

// Run your controlling app here. COORDINATES will be available in the global scope and will be updated constantly

EDIT: Created Method inside the get_coordinates function to re-establish socket connection in the event of a failure.

import socket
import os,sys
import time

HOST = '59.191.193.59'
PORT = 5555

# don't restrict yourself to IPv4 without any need:
client_socket = socket.create_connection((HOST,PORT))

def update_coordinates(client_socket):
    client_socket.send("loc\n")
    data = client_socket.recv(8192)
    coordinates = data.split()

    if(not(coordinates[-1] == "eom" and coordinates[0] == "start")):
        return None # or so

    return [int(i) for i in coordinates[1:-1]]

screen_width = 0
screen_height = 0

while True:
    coord = update_coordinates(screen_width)
    if coord is None: continue
    # ! indexes have changed...
    if (screen_width != coord[1]):
        screen_width = coord[1]
        screen_height = coord[2]
    print coordinates[7]
    print coordinates[8]
    print coordinates[11]
    print coordinates[12]

I'm not sure if I got the question right, though...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM