Book Image

Python GUI Programming Cookbook

By : Burkhard Meier
Book Image

Python GUI Programming Cookbook

By: Burkhard Meier

Overview of this book

Table of Contents (18 chapters)
Python GUI Programming Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using TCP/IP to communicate via networks


This recipe shows you how to use sockets to communicate via TCP/IP. In order to achieve this, we need both an IP address and a port number.

In order to keep things simple and independent of the ever-changing internet IP addresses, we will create our own local TCP/IP server and, as a client, learn how to connect to it and read data from a TCP/IP connection.

We will integrate this networking capability into our GUI by using the queues we created in previous recipes.

Getting ready

We will create a new Python module, which will be the TCP server.

How to do it...

One way to implement a TCP server in Python is to inherit from the socketserver module. We subclass BaseRequestHandler and then override the inherited handle method. In very few lines of Python code, we can implement a TCP server module.

from socketserver import BaseRequestHandler, TCPServer

class RequestHandler(BaseRequestHandler):
    # override base class handle method
    def handle(self):
    ...