-2
import sys
import re
import SocketServer
from sys import stdin
class MyTCPHandler(SocketServer.BaseRequestHandler):
"""
The RequestHandler class for our server.

It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""

def handle(self):
    server_msg = "Enter name:\n"
    sent = self.request.send(server_msg);
    self.handle_buffer = self.request.recv(1024).strip()
    server_msg = "That's a nice name\n"
    self.request.send(server_msg);

quit = 0
while quit < 1:
        print "Test Print 2" 

        # self.request is the TCP socket connected to the client
        self.client_response = self.request.recv(500).strip()
        self.handle_buffer.strip()
        self.client_response.strip()

        x = self.handle_buffer.split("\n", 1)
        y = self.client_response.split("\n",1)
        a = x[0]
        b = y[0]
        print a + "> " + b;
        sys.stdout.write(' You> ')
        sys.stdout.flush()
        server_msg = stdin.readline()
        if(server_msg is "\quit"):
            quit = 1;
            print "Test Print 1";
            self.request.send("The client has decided to close this connection. Good day.")
            self.request.close()
            quit = 1
            break;
        else:
            self.request.send(server_msg)
            print "Awaiting response from client..."



if __name__ == "__main__":
    HOST, PORT = "localhost", 9004

    # Create the server, binding to localhost on port 9004
    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)

    # Activate the server; this will keep running until you
    # interrupt the program with Ctrl-C
    server.serve_forever()

The if loop if(server_msg is "\quit"): is supposed to first print "Test Print 1" and then send the closing message to the client program and then close the socket and break the while loop. Instead, it doesn't print out "Test Print 1" at all, but it does successfully send the "The client has decided to close this connection. Good day." message (I should have said server, not client) and close the socket on the client's end, and then it continues the loop, and these are the results in the command line when \quit is entered:

You> \quit
Awaiting response from client...
Test Print 2
client_name>
You>

So it appears that \quit only causes two of the lines in the if statement to execute, and for some reason the else statement also runs? This doesn't make any sense to me. Any help appreciated.

GAVD
  • 1,977
  • 3
  • 22
  • 40
Lily Potter
  • 203
  • 2
  • 8
  • 2
    Probably your indentation is wrong. Also, semicolons to end statements in Python are pointless. Don't clutter your code with them. – nobody Jul 20 '16 at 00:30
  • It got copied down wrong in my question but in the actual code I checked it all by using `:set list` in vim and it looks the same. It's most bewildering that "test print 1" is never printed. – Lily Potter Jul 20 '16 at 00:43
  • 1
    So fix the formatting here. We can't possibly help if you don't do that. – nobody Jul 20 '16 at 01:24

1 Answers1

0

Are you sure that the server received this string - "The client has decided to close this connection. Good day."?

The problem with your if statement is the "is" comparison, which compares whether the two variables point to the same object, as opposed to their values.

Replacing it as follows works:

if server_msg == "\quit":

For more information, refer to this question: Is there a difference between `==` and `is` in Python?

Community
  • 1
  • 1