Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #49779 > unrolled thread

socket data sending problem

Started byollietempleman@aol.com
First post2013-07-03 15:38 -0700
Last post2013-07-04 00:02 +0100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  socket data sending problem ollietempleman@aol.com - 2013-07-03 15:38 -0700
    Re: socket data sending problem Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2013-07-04 00:57 +0200
    Re: socket data sending problem MRAB <python@mrabarnett.plus.com> - 2013-07-04 00:02 +0100

#49779 — socket data sending problem

Fromollietempleman@aol.com
Date2013-07-03 15:38 -0700
Subjectsocket data sending problem
Message-ID<dff7d1f8-183d-42c3-b921-73875e431ccd@googlegroups.com>
im trying to do a simple socket test program for a school project using the socket module, but im having difficulty in sending data between the client and host program.

so far all tutorials and examples have used something along the lines of:

  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  host = socket.gethostname()
  port = 12345
  s.connect((host, port))


and received it on the server end with:

  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  host = ''
  port = 12345
  s.bind((host, port))
  s.listen(1)
  conn, addr = s.accept()
  print ('client is at', addr)
  data = conn.recv(5)
  print(data)

it all works fine, except for when i try to use:

  s.send("hello")

to send data between the client and server, i just get this error message:

  >>> 
  Traceback (most recent call last):
    File "C:/Users/Ollie/Documents/code/chatroom/client3.py", line 9, in <module>
      s.send("hello")
  TypeError: 'str' does not support the buffer interface
  >>> 

if anyone can either show me what im doing wrong, what this means and what's causing it, or even better how to fix it it would be greatly appreciated

many thanks Ollie

[toc] | [next] | [standalone]


#49782

FromIrmen de Jong <irmen.NOSPAM@xs4all.nl>
Date2013-07-04 00:57 +0200
Message-ID<51d4ac5d$0$15982$e4fe514c@news.xs4all.nl>
In reply to#49779
On 4-7-2013 0:38, ollietempleman@aol.com wrote:

> it all works fine, except for when i try to use:
> 
>   s.send("hello")
> 
> to send data between the client and server, i just get this error message:
> 
>   >>> 
>   Traceback (most recent call last):
>     File "C:/Users/Ollie/Documents/code/chatroom/client3.py", line 9, in <module>
>       s.send("hello")
>   TypeError: 'str' does not support the buffer interface
>   >>> 

Are you using Python 3.x? Try sending bytes instead of strings:

 s.send(b"hello")

Or switch to using Python 2.7

Irmen

[toc] | [prev] | [next] | [standalone]


#49783

FromMRAB <python@mrabarnett.plus.com>
Date2013-07-04 00:02 +0100
Message-ID<mailman.4188.1372892534.3114.python-list@python.org>
In reply to#49779
On 03/07/2013 23:38, ollietempleman@aol.com wrote:
> im trying to do a simple socket test program for a school project using the socket module, but im having difficulty in sending data between the client and host program.
>
> so far all tutorials and examples have used something along the lines of:
>
>    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>    host = socket.gethostname()
>    port = 12345
>    s.connect((host, port))
>
>
> and received it on the server end with:
>
>    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>    host = ''
>    port = 12345
>    s.bind((host, port))
>    s.listen(1)
>    conn, addr = s.accept()
>    print ('client is at', addr)
>    data = conn.recv(5)
>    print(data)
>
> it all works fine, except for when i try to use:
>
>    s.send("hello")
>
> to send data between the client and server, i just get this error message:
>
>    >>>
>    Traceback (most recent call last):
>      File "C:/Users/Ollie/Documents/code/chatroom/client3.py", line 9, in <module>
>        s.send("hello")
>    TypeError: 'str' does not support the buffer interface
>    >>>
>
> if anyone can either show me what im doing wrong, what this means and what's causing it, or even better how to fix it it would be greatly appreciated
>
You didn't say which version of Python you're using, but I think that
you're using Python 3.

A socket handles bytes, not Unicode strings, so you need to encode the
Unicode strings to bytes before sending, and decode the bytes to
Unicode strings after receiving.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web