2

Hi guys I start learning python a month ago so excuse me for any stupid mistake. I recently make this chat server and it works find, but the messages that client receive, instead or have the form Hi, there, they look like this b'Hi, there\r\n', like a binary message, also because of this the \n and \r dont take effect. I post here my code, so if somebody could help me out it would be great, thanks. If someone want to test it the clients connect through telnet

import socket
import select
class ChatServer:
    def __init__( self, port ):
        self.port = port;
        self.srvsock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
        self.srvsock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
        self.srvsock.bind( ("", port) )
        self.srvsock.listen( 5 )
        self.descriptors = [self.srvsock]
        print ('ChatServer started on port %s' % port)
    def run( self ):
        while 1:
            # Await an event on a readable socket descriptor
            (sread, swrite, sexc) = select.select( self.descriptors, [], [] )
            # Iterate through the tagged read descriptors
            for sock in sread:
                # Received a connect to the server (listening) socket
                if sock == self.srvsock:
                    self.accept_new_connection()
                else:
                    # Received something on a client socket
                    str = sock.recv(100)
                    # Check to see if the peer socket closed
                    if str == b'exit\r\n':
                        host,port = sock.getpeername()
                        str = 'Client left %s:%s\r\n' % (host, port)
                        self.broadcast_string( str, sock )
                        sock.close
                        self.descriptors.remove(sock)
                    else:
                        host,port = sock.getpeername()
                        newstr = '[%s:%s] %s' % (host, port, str)
                        self.broadcast_string( newstr, sock )

    def broadcast_string( self, str, omit_sock ):
        sms=bytes(str,'utf-8')
        for sock in self.descriptors:
            if sock != self.srvsock and sock != omit_sock:
                sock.send(sms)
        print (str)


    def accept_new_connection( self ):
        newsock, (remhost, remport) = self.srvsock.accept()
        self.descriptors.append( newsock )
        newsock.send(b"You're connected to the Python chatserver, enter 'exit' to get out\r\n")
        str = 'Client joined %s:%s\r\n' % (remhost, remport)
        self.broadcast_string( str, newsock )

myServer = ChatServer( 2626 )
myServer.run()

1 Answer 1

1

Let's assume the variable str is your binary string.

You can convert it in the way you specified like this:

str = str.decode('utf-8').strip('\r\n')

If this doesn't work, please tell me!

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

7 Comments

I dont understand where do you suggest me to use this, please could you specify me, thanks
Right before the line that says host,port = sock.getpeername() you should put str = '\r\n'.join(str.decode('utf-8'))
I might also mention that str is a bad variable name because it overwrites the builtin function str()
Thanks @Qwery it really help your answer, the chat is working perfectly now, but instead of using '\r\n'.join(s.decode('utf-8')) I use ''.join(s.decode('utf-8')), because the way you say me the program put between each sting in str the \r\n. But thanks a lot for your help
@user652704 sorry I was doing it wrong, it should be strip not join, I will edit
|

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.