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.