本文实例讲述了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程序设计有所帮助。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]