0

i have this opensource code which is used to echo back response to web-socket requests:

_GOODBYE_MESSAGE = 'Goodbye'


def web_socket_do_extra_handshake(request):
    pass  # Always accept.


def web_socket_transfer_data(request):
while True:
    line = request.ws_stream.receive_message()
    if line == "hello":
        request.ws_stream.send_message("hello was sent")
    if line == "bye":
        request.ws_stream.send_message("bye was sent")
    if line is None:
        return
    #request.ws_stream.send_message(line)

    if line == _GOODBYE_MESSAGE:
        return

now problem is i want to modify it (transfer_data method) so that inside the while loop lets say it checks the string line if it equals certains text, it should return something else to client and if line equals something else it shoud return a different string. I have tried lot but it does not seem to work, i know it is very basic, but can someone please help me with this.
Another thing i want to do is to be able to add delay to response say 5 seconds, but import time does not work. I get error, please help with this.

Johnydep
  • 6,027
  • 20
  • 57
  • 74
  • 3
    Can you post the code that didn't work? – Owen Jul 25 '11 at 17:22
  • Ok i have modified it, i used something like the one i showed above. I am not familiar with python syntax, but what could be wrong with this? Also how can i add delay in printing result?? – Johnydep Jul 25 '11 at 17:34
  • What error message does the above code give you? (Besides the indentation issue between the def line and the beginning of the while loop, which I assume isn't in the original code) Also, FYI you can probably use an "if...elif...elif" structure for checking what line is rather than "if...if...if". – dpitch40 Jul 25 '11 at 17:36
  • Hi, actually it is working, i was confused with double errors. I want to add time delay but where should i put this import time, time.sleep(5) statements. – Johnydep Jul 25 '11 at 17:40

2 Answers2

3

for your first question, you can just say

if line == "whatever":
   # do stuff here, return, whatever...
   request.ws_stream.send_message(line)
else:
   # do something else....

for sleeping, you want

 import time
 time.sleep(seconds)

if the "import time" line fails, you've got a problem with your python interpreter configuration.

Paul Sanwald
  • 10,899
  • 6
  • 44
  • 59
  • thank you, the first part works, but the time sleep function has problems, it does not work – Johnydep Jul 25 '11 at 17:35
  • Another SO question that talks about python's time.sleep(): http://stackoverflow.com/questions/510348/how-can-i-make-a-time-delay-in-python – yasouser Jul 25 '11 at 17:40
  • [2011-07-25 19:40:43,058] [WARNING] root: mod_pywebsocket: ../example/echo_wsh.py: Error in sourcing handler:Traceback (most recent call last): File "C:\Python26\lib\site-packages\mod_pywebsocket\dispatch.py", line 120, in _source_handler_file exec handler_definition in global_dic File "", line 44 request.ws_stream.send_message("hello was sent") ^ IndentationError: unindent does not match any outer indentation level – Johnydep Jul 25 '11 at 17:43
  • this is the error that i get when i use sleep func anywhere inside if condition. – Johnydep Jul 25 '11 at 17:44
  • that error means your spacing is incorrect. python cares about whitespace, your time.sleep line must be indented the same as everything else inside your loop. – Paul Sanwald Jul 25 '11 at 17:48
  • Don't use `is` to check for equality, string or otherwise. –  Jul 25 '11 at 17:50
  • the spacing is perfectly fine. i don't understand why would it be related to indentation?? – Johnydep Jul 25 '11 at 17:53
  • the error you posted is an indentation error on line 44: line 44 request.ws_stream.send_message("hello was sent") ^ IndentationError: unindent does not match any outer indentation level – Paul Sanwald Jul 25 '11 at 17:54
  • this is strange, because if i remove the time.sleep statement while leave import time statement at the start, there is NO error. But with this time.sleep i get this error. Can you please help me – Johnydep Jul 25 '11 at 17:56
  • HI PROBLEM IS SOLVED. IT WAS THE PLACEMENT OF TIME.SLEEP I WAS PUTTING IT IN IF CONDITION AND IT DID NOT WORK. WHEN I PUT IT RIGHT AFTER WHILE IT WORKS. THANK YOU FOR YOUR HELP – Johnydep Jul 25 '11 at 18:00
0

For the control logic, look at the Python documentation (link is to 2.7).

Note you can modify your if structure to something like this:

def web_socket_transfer_data(request):
    while True: # This was at the wrong indent - check it was a copy-paste issue
        line = request.ws_stream.receive_message()

        if line is "hello":
            request.ws_stream.send_message("hello was sent")
        elif line is "bye": # elif is the Pythonic form of else if
            request.ws_stream.send_message("bye was sent")
        elif line is _GOODBYE_MESSAGE or line is None:
            break # This exits the while loop and by extension the method

        time.sleep(5)

As for the issue with time.sleep(), you'll want to make sure you do import time at the beginning of the file. If that doesn't work you might want to check if you can import it directly through IDLE. That is, run IDLE and type import time. If that fails, please post the error it returns.

thegrinner
  • 11,546
  • 5
  • 41
  • 64
  • with IDLE, there is no error. It accepts the command. I use import time at the start of this file. but still it gives following error on cmd line: – Johnydep Jul 25 '11 at 17:49
  • [2011-07-25 19:48:39,503] [WARNING] root: mod_pywebsocket: ../example/echo_wsh.py: Error in sourcing handler:Traceback (most recent call last): File "C:\Python26\lib\site-packages\mod_pywebsocket\dispatch.py", line 120, in _source_handler_file exec handler_definition in global_dic File "", line 43 request.ws_stream.send_message("hello was sent") ^ IndentationError: unindent does not match any outer indentation level – Johnydep Jul 25 '11 at 17:50
  • @Johnydep That last bit is the relevant part. The `IndentationError` means it's seeing something strange in your whitespace at line 43 - make sure your `while` loop is indented once from the method definition and that the if statements are two indents deep, with their actions 3 deep. Assuming you've place `time.sleep` about where I did it should be 2 deep (on the level with the if statements). Double-check there isn't mixed tabs and spaces. – thegrinner Jul 25 '11 at 17:56
  • i don't know. I just moved time.sleep from inside of if to right after while and now everything is working fine. – Johnydep Jul 25 '11 at 18:01
  • @Johnydep Sounds like it was definitely something at a different indent reacting oddly. – thegrinner Jul 25 '11 at 19:45