Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have recently started looking into networking with Python. I saw some tutorials about the socket library so I decided to give a "live chat room" idea a go. It works (on Windows, os.system("cls") may be problematic) but was just wanting to see how people could improve upon it

server.py

from socketserver import ThreadingTCPServer, BaseRequestHandler
from threading import Thread
import pickle,time,datetime,os


messages = []
temp = []
os.system("cls")
class Echo(BaseRequestHandler):


    def handle(self):
        self.temp = []
        Thread(target=self.send).start()
        self.username = self.request.recv(8192)
        self.username = self.username.decode()
        print("Got connection from {}:{}".format(self.client_address[0],
                                                 self.client_address[1]))

        while True:
            msg = self.request.recv(8192)
            msg = "[{} {}]: {}".format(datetime.datetime.now().strftime("%H:%M:%S"),
                               self.username,
                               msg.decode())

            messages.append(msg)
            print(msg)



            if not msg:
                break


    def send(self):

        global temp, messages
        while 1:


            if len(self.temp) != len(messages):

                data_string = pickle.dumps(messages)
                self.request.send(data_string)
                self.temp = [item for item in messages]
if __name__ == "__main__":
    serv = ThreadingTCPServer(("",20000), Echo)
    serv.serve_forever()

client.py

import socket,pickle,os
from threading import Thread
import time
s = socket.socket()
s.connect(('localhost',20000))
def receive():

    while True:

        data = s.recv(8192)
        data = pickle.loads(data)
        os.system("cls")
       for item in data:
            print(item)


username = input("Enter your username: ")
Thread(target=receive).start()

s.send(username.encode())
time.sleep(0.1)
while True:

    msg = input(">>")

    if not msg:
       break
    s.send(msg.encode())
    time.sleep(0.1)
share|improve this question
    
Your indentation within the while True loop seem s to be off. The way it is, it will run for ever. – Graipher Aug 14 at 17:50
    
@Graipher A server is usually supposed to run forever. Do you imply the break is not going to have the intended effect? – Mast Aug 14 at 18:07
    
@Mast I think he or she was pointing out that the break is not inside the loop with the current indentation. The def send seems wrong too, as it doesn't match def handle. – mdfst13 Aug 14 at 18:16
    
Yes, this is a error on my behalf, my terrible copy and pasting :) – cheiffeast Aug 14 at 18:18
    
I have read some places that using pickle over TCP can be dangerous, if this is true, what other methods can i use to send a list over TCP? – cheiffeast Aug 14 at 18:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.