I’m using python 3.3.this is Server.py.Everything is fine both server and client are able to connect
something might be wrong in here ‘tcpcli.send(‘[%s]%s’%(bytes(ctime(),’utf-8′),data))’.help me out
from socket import *
from time import ctime
HOST=''
PORT=21567
BUFSIZ=1024
ADDR=(HOST,PORT)
tcp=socket(AF_INET,SOCK_STREAM)
tcp.bind(ADDR)
tcp.listen(5)
while True:
print('waiting for connection')
tcpcli,addr=tcp.accept()
print('...connected from:',addr)
while True:
data=tcpcli.recv(BUFSIZ)
if not data:
break
tcpcli.send('[%s]%s'%(bytes(ctime(),'utf-8'),data))
tcpcli.close()
tcp.close()
This is CLient.py
from socket import *
HOST='127.0.0.1'
PORT=21567
BUFSIZ=1024
ADDR=(HOST,PORT)
tcpcli=socket(AF_INET,SOCK_STREAM)
tcpcli.connect(ADDR)
while True:
data=input('>')
if not data:
break
tcpcli.send(data)
data=tcpcli.recv(BUFSIZ)
if not data:
break
print (data.decode('utf-8'))
tcpcli.close()
When i’m running both they are working fine except I’m unable to send any data from client.
I’m getting this error message.
tcpcli.send(data)
TypeError: 'str' does not support the buffer interface
You are building unicode strings, not byte strings, and the socket interface doesn’t support unicode strings. You’ll need to encode the result of the string interpolation: