[Tutor] a problem with a server and client
Evert Rol
evert.rol at gmail.com
Fri Apr 6 15:59:17 CEST 2012
> i'm trying to implement a server that adds a time stamp to incoming text form a client.
>
> the server's code is (but doesn't seem to have the problem as demoed by the error below:
>
> from socket import *
> from time import ctime
>
> HOST = ''
> PORT = 21567
> BUFSIZ = 1024
> ADDR =(HOST, PORT)
>
> tcpSerSock = socket(AF_INET, SOCK_STREAM)
>
> tcpSerSock.bind(ADDR)
> tcpSerSock.listen(5)
>
> while True:
> print('waiting for connection ...')
> tcpCliSock, addr =tcpSerSock.accept()
> print('...connected from: ', addr)
>
> while True:
> data = tcpCliSock.recv(BUFSIZ)
> if not data:
> break
> tcpCliSock.send('[{}] {}'.format(bytes(ctime(), 'utf-8'),data))
>
> tcpCliSock.close()
> tcpSerSock.close()
>
>
>
> the client's code is:
>
> from socket import *
>
>
> HOST = 'localhost'
> PORT = 21567
> BUFSIZ = 1024
> ADDR =(HOST, PORT)
>
> tcpCliSock = socket(AF_INET, SOCK_STREAM)
>
> tcpCliSock.bind(ADDR)
>
> while True:
> data=input('> ')
> if not data:
> break
> tcpCliSock.send(data)
> data = tcpCliSock.recv(BUFSIZ)
> if not data:
> break
> print(data.decode('utf-8'))
>
> tcpCliSock.close()
>
> the problem is i get the following error when i enter some text:
>
> Traceback (most recent call last):
> File "C:\Python32\tsTclnt3.py", line 17, in <module>
> tcpCliSock.send(data)
> TypeError: 'str' does not support the buffer interface
Did you try to search on the error string? That would have gotten you the solution (even) faster.
The first two Google hits (and probably all the rest of them), tell me that Python 3's socket.send() method wants bytes as input, not str. See http://docs.python.org/py3k/library/socket.html#socket.socket.send
Hope that helps,
Evert
>
> can you help?
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list