DDR爱好者之家 Design By 杰米

本文实例讲述了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='<"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<"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='<"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<"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<"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程序设计有所帮助。

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?