One question per day _python. Using yield generator to implement the TPS transparent transfer CS Test under the co-process

Source: Internet
Author: User

Specific requirements:
1. Analog device first sends the registration package registered to the TPS server, then the client sends a private packet to the TPS, and the test device can receive a private packet to return the success or the flag bit (0, ') or (1, ' errormsg ')
2. Since this plug-in is used for its own write plug-in monitoring system, so the entry function name must be consistent with the file name, which is tentatively server_tps_status.py

Implementation ideas:

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/86/E7/wKioL1fOcGiysTXhAAAyvosIF9Q849.png-wh_500x0-wm_3 -wmp_4-s_4271378686.png "title=" service_tps_status.py "alt=" Wkiol1focgiystxhaaayvosif9q849.png-wh_50 "/>


Specific code:

#!/usr/bin/env python# -*- coding: utf-8 -*-"" "## authors: limanman#  oschina: http://xmdevops.blog.163.com/# purpose:# install:# "" "#  description:  Importing public module import  JSONIMPORT SOCKETDEF DEVICE_SEND_RECV (device_socket, tps_host, tps_port,  Private_data, extra_data):    send_data = {          "Transproxy": {             "Body": {                 ' Authcode ':  ' 0000000000000000 ',                  ' serialnumber ':  ' 0000000000000000 '              },             ' Header ':  {   &NBsp;             ' CSeq ':  ' 1 ',                  ' MessageType ':  ' msg_ Transproxy_register_req ',                  ' Terminaltype ':  ' Camera ',                  ' Version ':  ' 1.0 '              }        }    }     send_data = json.dumps (Send_data)     send_data =  ("POST  /transproxy http/1.1\r\n "                   "host:%s\r\n"                   "Port:%s\r\n"                    "connection:keep-alive\r\n"                    "content-length:%s\r\n\r\n"                    "%s\r\n"                   )  %  (Tps_host, tps_port, len (send_ data),  send_data)     device_socket.send (send_data)     while  TRUE:        CUR_BUFFER = DEVICE_SOCKET.RECV (1024x768)          if  ' Transproxy '  in cur_buffer:             yield  (0,  ')          elif private_data in cur_buffer:            yield   (0,  ')             break         else:             yield  (1, cur_buffer)              BREAKDEF CLIENT_SEND_RECV (Device_socket, tps_host, tps_port, private_data, extra_data ):    send_data =  ("post /privatedata http/1.1\r\n"                    "host:%s\r\n"                    "port:%s\r\n"                    "authcode:%s\r \ n "                  "SrcUuid:%s\r\n"                    "destuuid:%s\r\n"                   " connection:keep-alive\r\n "                   "content-length:%s\r\n\r\n"                    "%s\r\n"                   )  %  (tps_host, tps_port, extra_data[0],  extra_data[1],                       extra_data[2], len (Private_data),  private_data)      device_socket.send (SENd_data) def service_tps_status ():    private_data =  ' Xmdevops '      exec_ret_dicts = {' status ': 0,  ' target ': 0,  ' errors ':  '}     tps_host, tps_port =  ' 127.0.0.1 ',  6604    extra_data  =  (         ' 02899574bd6e899060 ',          ' 1111111111111111 ',         ' 0000000000000000 ',     )     client_socket = socket.socket (socket.af_inet,  Socket. SOCK_STREAM)     device_socket = socket.socket (socket.af_inet, socket. SOCK_STREAM)     try:        client_socket.connect ( (Tps_host, tps_port))         device_socket.connect ((Tps_host, tps_port)) &Nbsp;   except socket.error, e:        exec_ Ret_dicts.update ({             ' status ':  1,              ' target ': 1,              ' Errors ': e.strerror,         })         return exec_ret_dicts     DEVICE = DEVICE_SEND_RECV (Device_socket, tps_host, tps_port, private_data ,  extra_data)     device.next ()     client_send_recv (Client_socket,  tps_host, tps_port, private_data, extra_data)     while true:         try:             result  = device.next ()         except StopIteration, e:             break    exec_ret_ Dicts.update ({         ' status ': result[0],          ' target ': result[0],         ' errors ':  result[1],    })     return exec_ret_dictsif __name__  ==  ' __main__ ':     print (Service_tps_status ())



This article is from the "ζ Automated operation and maintenance development Road ζ" blog, please be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1846887

One question per day _python. Using yield generator to implement the TPS transparent transfer CS Test under the co-process

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.