python怎么使用socket先读取长度,在读取报文内容

发布时间:2021-03-30 09:41:04 作者:小新
来源:亿速云 阅读:594

这篇文章主要介绍了python怎么使用socket先读取长度,在读取报文内容,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

本文实例讲述了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怎么使用socket先读取长度,在读取报文内容”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. 最大报文段长度(MSS)
  2. 使用Python怎么逐行读取文件内容

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python socket

上一篇:python如何使用 __init__初始化

下一篇:Python如何使用grequests(gevent+requests)并发发送请求

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》