# Python socket sending and receiving

**URL:** https://discourse.vtk.org/t/python-socket-sending-and-receiving/1035
**Category:** Support
**Created:** [May 28, 2019, 3:23pm UTC](https://discourse.vtk.org/t/python-socket-sending-and-receiving/1035 "2019-05-28T15:23:17Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![tom](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/3d9bf3/32.png) [@tom](https://discourse.vtk.org/u/tom)
#### Post date: [May 28, 2019, 3:23pm UTC](https://discourse.vtk.org/t/python-socket-sending-and-receiving/1035/1 "2019-05-28T15:23:17Z")

</div>

Hi,

I’m trying to write a Python script that sends data over a vtk socket. I can’t work out how the Python bindings for the Send() and Receive() methods work. If I try and pass a python string as the first argument, I get “TypeError: Send argument 1: requires a \_addr\_p\_void string”. If I try and pass anything else, I get “TypeError: Receive argument 1: object does not have a readable buffer”. I have similar problems with Receive(). How are these methods supposed to be used in Python?

On a related note, is there any documentation for the Python bindings (besides what’s written by the help() function)?

Thanks,  
Tom

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [May 28, 2019, 9:03pm UTC](https://discourse.vtk.org/t/python-socket-sending-and-receiving/1035/2 "2019-05-28T21:03:57Z")

</div>

The Send() and Receive() methods need to be given a Python object that supports the “buffer protocol”, e.g. a python “bytes” object, a “bytearray” object, or a numpy array. The python buffer protocol is a set of C APIs that allows Python extensions like numpy or vtk to directly access the storage space that is used by a Python object.

A python string object (in Python 3, at least) does not support the buffer protocol, since the internal encoding of the string is private.

See the [README](https://gitlab.kitware.com/vtk/vtk/blob/master/Wrapping/Python/README.md) for more info. The short story is, the Python wrappers provide a translation layer between C `void *` and the Python buffer protocol.

---

<div class="post-metadata">

### Author: ![tom](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/3d9bf3/32.png) [@tom](https://discourse.vtk.org/u/tom)
#### Post date: [May 30, 2019, 9:13am UTC](https://discourse.vtk.org/t/python-socket-sending-and-receiving/1035/3 "2019-05-30T09:13:59Z")

</div>

Thanks for your quick reply - it worked, but I still had problems. I dropped in to C++ to debug these problems, and I’ve ended up starting a new thread here: [C++ Socket Problems](https://discourse.vtk.org/t/c-socket-problems/1049)
