Python socketserver Framework Programming

Source: Internet
Author: User

1. Socketserver Framework

In Python, Socketserver is an integrated module that has the following features:
  

    • You can create TCP and UDP servers by using the Socketserver framework.
    • In the background for you to do a good job of every basic step.
    • You can manually create a server to spoof the client and analyze its behavior.
2. Use of the Socketserver module
    • Must be a subclass of Baserequesthandler
    • overriding the handle () function
    • Call handle_request or Serve_forever to handle the client program.
    • For the TCP server,
      • Self.request is the client socket
      • Self.client_address is the client's detailed address
3. Python Code

Service side:

#!/usr/bin/env python# _*_ Coding=utf-8 _*_ImportSocketserver class sockhandler(socketserver.baserequesthandler):     def handle(self):        Print "received a connection to the client:", self.client_address data =' Start '         whileLen (data): data = SELF.REQUEST.RECV (2048) Self.request.send ("return:"+ data)Print "Client off ..."SERADDR = ("0.0.0.0",8888)Print "Waiting for connection ..."Server = Socketserver.tcpserver (seraddr,sockhandler) server.serve_forever ()

The client tests with the NC command:

$ nc 192.168.1.95 8888

How are you doing
Back to: Hello
Okay
return: Okay

Python socketserver Framework Programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.