[Tutor] a problem with a server and client
Khalid Al-Ghamdi
emailkgnow at gmail.com
Fri Apr 6 15:44:10 CEST 2012
hi,
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
can you help?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120406/cd7aa4d8/attachment.html>
More information about the Tutor
mailing list