0

How would i loop every 5 seconds the following code :

sendSocket = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
sendSocket.connect ( ( 'CENSORED', 1234 ) )
sendSocket.send ( 'request posit' )
data = sendSocket.recv( 100 )
chatLog.insert(END, data)

I want a client to recieve data every 5 seconds from a server, i did a while True loop (lol) and the program interface didnt even load and the server got flooded by the client.

I just want the server to update the client every 5 seconds with a variable that is stored on the server, without a user having to manually press a button.

import socket, time
from Tkinter import *

## Main Window
gui = Tk()
gui.geometry('450x350-5+40')
gui.minsize(450,350)
gui.maxsize(450,350)
userInput = StringVar()

## Main window that displays user and server input.
chatLog = Text(gui, width=60, height=15)
chatLog.pack()

## Send Packet function, main part of the script. Sends whatever the user puts in.
def sendChat():
    sendSocket = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    sendSocket.connect ( ( 'nope.com', 1234 ) )
    sendSocket.send ( e.get() )
    data = sendSocket.recv( 100 )
    chatLog.insert(END, data)
    sendSocket.close()

while True:
    sendSocket = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    sendSocket.connect ( ( 'nope.com', 1234 ) )
    sendSocket.send ( 'request posit' )
    data = sendSocket.recv( 100 )
    chatLog.insert(END, data)
    sendSocket.close()
    time.sleep (5.0)

## Submit text button
b = Button(gui, text="Send", command=sendChat )
b.pack()

## Text entry box
e = Entry(gui, textvariable=userInput)
e.pack()

gui.mainloop()
4
  • Why did you feel the need to randomly insert (lol) into the middle of that? Commented May 30, 2011 at 1:46
  • The answer to this question depends on how you are creating your program interface. What did you use wxPython, PyQt, PySide, curses, Gtk? Commented May 30, 2011 at 1:47
  • Because i know while True is definately not the correct way to go about doing something properly. Also i used tkinter to make the GUI. Commented May 30, 2011 at 1:49
  • 1
    (lol) in my opinion makes you look silly. I at least takes questions more seriously if you don't do that. Commented May 30, 2011 at 2:04

2 Answers 2

2

See this question:

How to make tkinter repond events while waiting socket data?

You can also use after_idle

def read_the_socket():
    sendSocket.send ( 'request posit' )
    data = sendSocket.recv( 100 )
    gui.after(5000, read_the_socket)


gui.after_idle(read_the_socket)

after_idle schedules a function the be called once the GUI isn't busy anymore. You can also use .after(time, function) to delay for a specific amount of time before calling the function again.

Finally, you should really maintain your connection to the server not reconnect each time.

Sign up to request clarification or add additional context in comments.

3 Comments

after(delay_ms, callback, args...) is probably better here, since after_idle will get called alot.
This is good and kind of works but after 1 time i get [Errno 10054] An existing connection was forcibly closed by the remote host
@Brandon, is the server yours? The server has to be programmed to allow multiple requests for a single connection. Otherwise you need to include the whole connect/send/recv/close in the function.
1
import time

while True:
    do_stuff ()
    time.sleep (5.0)

Ideally after do_stuff up you would calculate how long you had been asleep for, then adjust your sleep time as appropriate to get back into sync.

Edit: This assumes that's all you're doing. Otherwise use the time functions to see if it's been 5 secs. time.localtime() or something. I don't remember their names, but python's libs are well documented.

Edit 2: Although that might not apply to tkinter. Really i wouldnt have posted if the full code and tkinter had been in the original message. Sorry!

4 Comments

Ive done this, the server doesnt get flooded now but the GUI still doesn't load. Its made in tkinter. Ive edited the main post to show the entire code.
I don't know tkinter, sorry, can't help you with that. I can't see a problem with while True at all.
EDIT: although that might not apply to tkinter. Really i wouldnt have posted if the full code and tkinter had been in the original message. Sorry!
Ah thats my fault, sorry about that. I just assumed what i made it with didnt really matter and there would be a simple solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.