network programming and sockets an example of establishing udp connection appears in the socket module section later in this it is common for network protocols to exchange data in the form of text howevergreat attention needs to be given to text encoding in python all strings are unicode thereforeif any kind of text string is to be sent across the networkit needs to be encoded this is why the server is using the encode('ascii'method on the data it transmits likewisewhen client receives network datathat data is first received as raw unencoded bytes if you print it out or try to process it as textyou're unlikely to get what you expected insteadyou need to decode it first this is why the client code is using decode('ascii'on the result the remainder of this describes modules that are related to socket programming describes higher-level modules that provide support for various internet applications such as email and the web asynchat the asynchat module simplifies the implementation of applications that implement asynchronous networking using the asyncore module it does this by wrapping the low-level / functionality of asyncore with higher-level programming interface that is designed for network protocols based on simple request/response mechanisms (for examplehttpto use this moduleyou must define class that inherits from async_chat within this classyou must define two methodscollect_incoming_data(and found_terminator(the first method is invoked whenever data is received on the network connection typicallyit would simply take the data and store it someplace the found_terminator(method is called when the end of request has been detected for examplein httprequests are terminated by blank line for data outputasync_chat maintains producer fifo queue if you need to output datait is simply added to this queue thenwhenever writes are possible on the network connectiondata is transparently taken from this queue async_chat([sock]base class used to define new handlers async_chat inherits from asyncore dispatcher and provides the same methods sock is socket object that' used for communication an instanceaof async_chat has the following methods in addition to those already provided by the asyncore dispatcher base classa close_when_done(signals an end-of-file on the outgoing data stream by pushing none onto the producer fifo queue when this is reached by the writerthe channel will be closed collect_incoming_data(datacalled whenever data is received on the channel data is the received data and is typically stored for later processing this method must be implemented by the user discard_buffers(discards all data held in input/output buffers and the producer fifo queue lib fl ff