socket send

Mark Tolonen metolone+gmane at gmail.com
Thu Jul 30 09:56:07 EDT 2009


"NighterNet" <darkneter at gmail.com> wrote in message 
news:55aba832-df6d-455f-bf34-04d37eb061cd at i4g2000prm.googlegroups.com...
>I am trying to figure out how to send text or byte in python 3.1. I am
> trying to send data to flash socket to get there. I am not sure how to
> work it.
>
> buff= 'id=' , self.id , ':balive=False\n'
> clientSock.send(buff);
> -- 
> http://mail.python.org/mailman/listinfo/python-list
>

Python 3.1 strings are Unicode (think characters not bytes).  When writing 
to files, sockets, etc. bytes must be used.  Unicode strings have an 
encode() method, where you can specify an appropriate encoding (ascii, 
latin-1, windows-1252, utf-8, etc.):

    clientSock.send(buff.encode('ascii'))

When reading from the socket, you can decode() the byte strings back into 
Unicode strings.

    data = clientSock.recv(1024).decode('ascii')

-Mark





More information about the Python-list mailing list