[Tutor] a problem with a server and client

Khalid Al-Ghamdi emailkgnow at gmail.com
Fri Apr 6 16:19:07 CEST 2012


actually, you are right... in addition to that problem, there was the
encodeing/decoding issue you mentioned on both the client and server.

thanks

On Fri, Apr 6, 2012 at 5:08 PM, Khalid Al-Ghamdi <emailkgnow at gmail.com>wrote:

> yah i did the search, and tried the solution, but it didn't work.... nice
> of you to have tried, though...
>
> anyhow, i found where the problem is... on the client side it should be
> connect() instead of bind() in :
>
> > tcpCliSock.bind(ADDR)
>
> thanks
>
>
> On Fri, Apr 6, 2012 at 4:59 PM, Evert Rol <evert.rol at gmail.com> wrote:
>
>> > 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
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120406/5688e363/attachment.html>


More information about the Tutor mailing list