0

hi guys I try to build an simple client server application with Python3, that works quiet god, but i fail to format the text back to a 'nomral' string. I always get the 'b' prefex infront of my printed text.

The Client Code:

import socket

SERVER_PORT = 50007
BUFSIZE = 1024

def main():
    host = input("Serveradresse: ")
    print()
    print("Narchicht: ")
    msg = input()
    msg = bytes(msg, 'UTF-8')
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    s.connect((host, SERVER_PORT))

    s.sendall(msg)
    print("Sendet")
    print()
    x = input("Press enter to continue!")
    s.close()


if __name__== '__main__':
    main()

The server code:

import socket

ECHO_PORT = 50007
BUFSIZE = 1024

def main():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind(('', ECHO_PORT))
    s.listen(1)
    print("server Up") 
    conn, (remotehost, remoteport) = s.accept()

    print('Connected with' + remotehost)
    data = conn.recv(BUFSIZE)
    print("Echo: " + repr(data))
    while True:
        if data:
            data = conn.recv(BUFSIZE)
            value = repr(data)
            print("Echo: ", value)
    s.close()

if __name__== '__main__':
    main()

I'm shure, the solution will be easy but I am not abel to find it.

Thanks for Help.

v3nd3774
  • 213
  • 1
  • 2
  • 4
  • Hi guys i solved it, the solution code is: "".join(map(chr, data)) to decode it back – v3nd3774 May 05 '15 at 20:07
  • When you recv from socket in python3 you receive bytes, not a string. That is why you get the b prefix. You can do b"data".decode() or http://stackoverflow.com/questions/606191/convert-bytes-to-a-python-string – Robin Manoli Mar 09 '16 at 20:07

0 Answers0