makefile ( 'w' ))) # Send the data text = u'pi: π' print 'Sending :', repr ( text ) outgoing. connect (( ip, port )) # Wrap the socket with a reader and writer. start () # Connect to the server s = socket. setDaemon ( True ) # don't hang on exit t. server_address # find out what port we were given t = threading. TCPServer ( address, Echo ) ip, port = server. close () if _name_ = '_main_' : import codecs import socket import threading address = ( 'localhost', 0 ) # let the kernel give us a port server = SocketServer. flush () def close ( self ): return self. read ( size ) print repr ( data ) return data def flush ( self ): return self. write ( data ) def read ( self, size =- 1 ): print 'Reading :', data = self. other = other def write ( self, data ): print 'Writing :', repr ( data ) return self. send ( data ) return class PassThrough ( object ): def _init_ ( self, other ): self.
There is # no need to decode them, since they are not used. BaseRequestHandler ): def handle ( self ): # Get some bytes and echo them back to the client. Import sys import SocketServer class Echo ( SocketServer. You could encode the data explicitly, before sending it, but miss oneĬall to send() and your program would fail with an encoding recv ( len_sent ) print repr ( response ) # Clean up s. send ( text ) # Receive a response response = s. connect (( ip, port )) # Send the data text = u'pi: π' len_sent = s. send ( data ) return if _name_ = '_main_' : import codecs import socket import threading address = ( 'localhost', 0 ) # let the kernel give us a port server = SocketServer.
#Ff works copy codec code#
Represented by code points that do not fit into two bytes. Other encodings can be moreĮfficient for storing languages where most of the characters are Respectively to represent each character. UTF-16, which use sequences of one and two byte values The most common encodings for Western languages are UTF-8 and Incoming bytes can be converted to the internal representation used by Reading Unicode data also requires knowing the encoding so that the Value are not necessarily the same as the code point values, and theĮncoding defines a way to translate between the two sets of values. Of several standard schemes so that the sequence of bytes can be When unicode strings are output, they are encoded using one Both unicode and str are derived from aĬommon base class, and support a similar API. Of 2 or 4 bytes each, depending on the options given when Python wasĬompiled. The code point values are saved as a sequence Unicode strings are managed internally as a sequence of Old-style str instances use a single 8-bit byte to representĮach character of the string using its ASCII code. CPython 2.x supports two types of strings for working with text data.