[Tutor] a problem with a server and client

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] a problem with a server and client

Khalid Al-Ghamdi
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?

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] a problem with a server and client

Evert Rol
> 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  -  [hidden email]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] a problem with a server and client

Khalid Al-Ghamdi
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 <[hidden email]> 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  -  [hidden email]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] a problem with a server and client

Khalid Al-Ghamdi
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 <[hidden email]> 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 <[hidden email]> 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  -  [hidden email]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor




_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor