python使用socket 先读取长度,在读取报文内容示例

yipeiwu_com5年前Python基础

本文实例讲述了python使用socket 先读取长度,在读取报文内容。分享给大家供大家参考,具体如下:

tlpmts1:~/sbin # cat test9105.py

# -*- coding: utf-8 -*-
import SocketServer
import socket
import time
import sys
reload(sys)
print time.ctime()
sys.setdefaultencoding('utf-8')
class SMSsendx (object):
 def Sms(self):
   #.decode('utf-8').encode('gbk')
   host='10.4.160.71'
   port = 9105
   print 'host'+'->'+host
   print 'port'+'->'+str(port)
   bufsize=10240
   addr=(host,port)
   s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
   s.settimeout(20)
   s.connect((host,port))
   data=''
   # data='<?xml version="1.0" encoding="UTF-8"?><SERVICE><SYS_HEAD><USER_ID>007144</USER_ID><TRAN_TIMESTAMP>100443923</TRAN_TIMESTAMP><AUTH_USER_ID_ARRAY><AUTH_CHECK_FLAG></AUTH_CHECK_FLAG><AUTH_CHECK_MSG></AUTH_CHECK_MSG><AUTH_USER_ID></AUTH_USER_ID></AUTH_USER_ID_ARRAY><BRANCH_ID>330600600</BRANCH_ID><TRAN_DATE>20170920</TRAN_DATE><ORG_SYS_ID>101020</ORG_SYS_ID><SOURCE_TYPE>A01</SOURCE_TYPE><PROVIDER_ID>301020</PROVIDER_ID><CONSUMER_SEQ_NO>10102020170926010000550744</CONSUMER_SEQ_NO><WS_ID>10.4.129.21</WS_ID><SERVICE_SCENE>17</SERVICE_SCENE><SERVICE_CODE>01003000003</SERVICE_CODE><CONSUMER_ID>101020</CONSUMER_ID><FILE_PATH></FILE_PATH></SYS_HEAD><APP_HEAD><LKG_FLAG></LKG_FLAG><CURRENT_NUM>0</CURRENT_NUM><WNDW_NO>0</WNDW_NO><REVERSAL_DATE></REVERSAL_DATE><TOTAL_ROWS></TOTAL_ROWS><PSWD_KEY_INDX></PSWD_KEY_INDX><SEND_TRAN_CODE></SEND_TRAN_CODE><PSWD_ECPTN_FLG></PSWD_ECPTN_FLG><AHR_MODE></AHR_MODE><BUSS_SEQ_NO>10102020170926010000550744</BUSS_SEQ_NO><WARN_CFM_FLAG></WARN_CFM_FLAG><CORP_CODE>9999</CORP_CODE><TOTAL_NUM>14</TOTAL_NUM><FEE_CFM_FLAG></FEE_CFM_FLAG><LKG_TRAN_CODE></LKG_TRAN_CODE><SEND_TRAN_NAME></SEND_TRAN_NAME><TRAN_SCENE>0000000000</TRAN_SCENE><PGUP_OR_PGDN></PGUP_OR_PGDN><CHNL_FLG_STRG>10000000000000000000</CHNL_FLG_STRG><REVERSAL_SEQ_NO></REVERSAL_SEQ_NO><REVERSAL_TRAN_TYPE></REVERSAL_TRAN_TYPE></APP_HEAD><LOCAL_HEAD></LOCAL_HEAD><BODY><CURRENT_NUM>0</CURRENT_NUM><ACCT_NO>33010000201000000459</ACCT_NO><PASSWORD_TYPE>0</PASSWORD_TYPE><START_PAGE>1</START_PAGE><TOTAL_NUM>14</TOTAL_NUM><ACCT_SERIAL_NO></ACCT_SERIAL_NO><RET_CERT_SBU_ACCT_DETAIL_FLG>1</RET_CERT_SBU_ACCT_DETAIL_FLG><SVC_FIELD>0110111100</SVC_FIELD></BODY></SERVICE>'
   # length = len(data.encode('utf-8'))
   # slen = '%08d' % length
   # data=slen + data
   # data='000314<?xml version="1.0" encoding="UTF-8"?> <Esb> <head> <Serial>21801020180528010000218475</Serial> <Channel>L06</Channel> <DateTime>2018052810241220</DateTime> <HostCode>ES999</HostCode> <TermNo>APP</TermNo> <TxnUsr>ZXGY</TxnUsr> <TxnOrg>999999999</TxnOrg> <CONSUMER_ID>218010</CONSUMER_ID> </head> </Esb>'
   data='<?xml version="1.0" encoding="UTF-8"?> <SERVICE> <SYS_HEAD> <USER_ID>004355</USER_ID> <TRAN_TIMESTAMP>173906133</TRAN_TIMESTAMP> <BRANCH_ID>330100921</BRANCH_ID> <BACK_TXN_CD>ES999</BACK_TXN_CD> <CORP_CODE>9999</CORP_CODE> <TRAN_DATE>20190528</TRAN_DATE> <ORG_SYS_ID>202020</ORG_SYS_ID> <SOURCE_TYPE>M01</SOURCE_TYPE> <ESB_SEQ_NO>50010120190528010000002780</ESB_SEQ_NO> <PROVIDER_ID>201010</PROVIDER_ID> <VERSION></VERSION> <CONSUMER_SEQ_NO>20202020190528878173906123</CONSUMER_SEQ_NO> <MESSAGE_CODE>0001</MESSAGE_CODE> <SERVICE_SCENE>27</SERVICE_SCENE> <CONSUMER_ID>202020</CONSUMER_ID> <SERVICE_CODE>01003000003</SERVICE_CODE> </SYS_HEAD> <APP_HEAD> <BUSS_SEQ_NO>20202020190424878173906123</BUSS_SEQ_NO> </APP_HEAD> <LOCAL_HEAD></LOCAL_HEAD> </SERVICE>'
   length = len(data.encode('utf-8'))
   slen = '%08d' % length
   data=slen + data
   print data
   recv_size = 0
   #msg = data.encode('gbk')
   s.send(data)
   data = s.recv(bufsize)
   # print data.strip()
   length=data
   print int(length)
   resp = s.recv(int(length));
   cur_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
   print ("%s receice from server :%s") % (cur_time,data + resp)
a=SMSsendx()
a.Sms()
print time.ctime()

tlpmts1:~/sbin # python test9105.py
Thu Jul  4 16:06:43 2019
host->10.4.160.71
port->9105
00000754<?xml version="1.0" encoding="UTF-8"?> <SERVICE> <SYS_HEAD> <USER_ID>004355</USER_ID> <TRAN_TIMESTAMP>173906133</TRAN_TIMESTAMP> <BRANCH_ID>330100921</BRANCH_ID> <BACK_TXN_CD>ES999</BACK_TXN_CD> <CORP_CODE>9999</CORP_CODE> <TRAN_DATE>20190528</TRAN_DATE> <ORG_SYS_ID>202020</ORG_SYS_ID> <SOURCE_TYPE>M01</SOURCE_TYPE> <ESB_SEQ_NO>50010120190528010000002780</ESB_SEQ_NO> <PROVIDER_ID>201010</PROVIDER_ID> <VERSION></VERSION> <CONSUMER_SEQ_NO>20202020190528878173906123</CONSUMER_SEQ_NO> <MESSAGE_CODE>0001</MESSAGE_CODE> <SERVICE_SCENE>27</SERVICE_SCENE> <CONSUMER_ID>202020</CONSUMER_ID> <SERVICE_CODE>01003000003</SERVICE_CODE> </SYS_HEAD> <APP_HEAD> <BUSS_SEQ_NO>20202020190424878173906123</BUSS_SEQ_NO> </APP_HEAD> <LOCAL_HEAD></LOCAL_HEAD> </SERVICE>
539
2019-07-04 16:06:43 receice from server :00000539<?xml version="1.0" encoding="UTF-8"?><SERVICE><SYS_HEAD><RET><RET_CODE>0000</RET_CODE>
<RET_MSG>交易成功</RET_MSG>
</RET><TRAN_DATE>20190528</TRAN_DATE>
<TRAN_TIMESTAMP>173906133</TRAN_TIMESTAMP>
<SERVICE_CODE>01003000003</SERVICE_CODE>
<SERVICE_SCENE>27</SERVICE_SCENE>
<CONSUMER_SEQ_NO>20202020190528878173906123</CONSUMER_SEQ_NO>
<PROVIDER_ID>201010</PROVIDER_ID>
<CONSUMER_ID>202020</CONSUMER_ID>
</SYS_HEAD><APP_HEAD><BUSS_SEQ_NO>20202020190424878173906123</BUSS_SEQ_NO>
<TOTAL_NUM></TOTAL_NUM>
</APP_HEAD><BODY></BODY></SERVICE>
Thu Jul  4 16:06:43 2019

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

python optparse模块使用实例

使用命令行时,如果要添加选项的话,python 2.3里新增加了一个模块叫optparse,也是专门来处理命令行选项的。 复制代码 代码如下: from optparse import...

Python使用Flask-SQLAlchemy连接数据库操作示例

本文实例讲述了Python使用Flask-SQLAlchemy连接数据库操作。分享给大家供大家参考,具体如下: 需要安装flask pip install flask 安装My...

在vscode中配置python环境过程解析

在vscode中配置python环境过程解析

1.安装vscode和python3.7(安装路径在:E:\Python\Python37); 2.打开vscode,在左下角点击设置图标选择setting,搜索python path,...

python 拷贝特定后缀名文件,并保留原始目录结构的实例

如下所示: #!/usr/bin/python # -*- coding: UTF-8 -*- import os import shutil def cp_tree_ext(ex...

python实现爬山算法的思路详解

python实现爬山算法的思路详解

问题 找图中函数在区间[5,8]的最大值  重点思路 爬山算法会收敛到局部最优,解决办法是初始值在定义域上随机取乱数100次,总不可能100次都那么倒霉。 实现 imp...