QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2344|回复: 0
打印 上一主题 下一主题

tcp的socket连接报文测试工具

[复制链接]
字体大小: 正常 放大

1186

主题

4

听众

2923

积分

该用户从未签到

跳转到指定楼层
1#
发表于 2024-3-31 17:21 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
# -*- coding: utf-8 -*-
3 f4 g4 _' P0 R5 e' nimport socket
8 G9 ?  s0 i+ H. xfrom myutil import *8 b# y% O9 Q' g: O
from binascii import unhexlify as unhex
( A/ o  C) e/ F" r/ Y5 efrom ctypes import *% P) g. ~9 E3 y& q& v: z, [
dll = cdll.LoadLibrary('mydll.dll')
0 x- Y" M$ R$ A3 z: z8 T& lprint 'begin load mydll..'; J% G0 T2 J# n. j; g3 R
HOST, PORT = "192.168.51.28", 5800+ A9 M& C3 E& T5 J5 T
sd ="1234567812345678"* L) H5 H5 s" w! z% s
# Create a socket (SOCK_STREAM means a TCP socket); g: g# q; J* ?' v0 b4 h2 d
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
( o8 Q2 W) ?1 Q, ~1 W2 Xtry:( Q  K, r) r9 w1 s; {1 Y2 e
    # Connect to server and send data7 L, {7 @$ a4 E: i* [6 G
    sock.connect((HOST, int(PORT))
: i+ {$ ~* Q' Q( ?4 _    print "Sent1 OK:"! T+ X  D) f& w
    print sd/ M3 q2 f6 H) B3 w
    # Receive data from the server and shut down$ L" P( D7 Z5 K6 m; d, n
    received = sock.recv(1024)9 n( U/ i# |0 s- j: ~  T) `+ o
    print "Received:"
" [( @, g* O! ^    print_hex(received)
6 J& c6 L1 x' b( T    print  'received len is 0x%02x' %(len(received))! A" V8 ]: J3 p( f2 @  y* z
    print  'received data analysis...'
8 G/ q7 `+ L! A. c% u0 \3 w    re1=received[0:4]
( a; u* q. q# j3 h4 S    print_hex(re1)! ?8 s# e* u( e! f/ j+ d& g
    re1=received[4:6]
' y7 t' e! q1 l    print_hex(re1)! r% t, y9 o% A6 M% @
    re1=received[6:10]
; B! L( x- W, ?5 X5 T* \. I    print_hex(re1)5 J, e: f* D6 t) p- x
    re1=received[10:16]
& u# x4 S  o5 _* G- \! O" V    print_hex(re1)
& v, B5 b# m  o0 f) Z. V* S" ~
/ |5 E- l" ?* ~( d* R0 L8 B4 {1 S    #pack2 send
( G" S; S5 E; q2 \6 G6 |    sock.send(sd2.decode('hex'))
8 C! ~8 W" p* \  y3 n: s    print "Sent2 OK:"
# |1 Q1 `( V3 q# C% S+ A5 s    print sd2
& w2 w9 ?# f+ D. c    # Receive data from the server and shut down; p6 l- j" a7 e  b9 w( c
    received1 = sock.recv(1024)! [$ }$ F9 G9 c) {7 ^
    print "Received1:"! Z! |9 x$ b3 I
    print_hex(received1)( W6 |/ ?9 S8 I* O1 I9 ?( ?6 L$ L
    print  'received1 len is 0x%02x' %(len(received1))$ Q6 n9 y' @' k$ {
  x" c6 W9 U9 K) d2 D
finally:/ S0 I. @6 x4 X' `
    sock.close()
. W8 A& U1 f, h! f3 m
. X1 D( {0 j, S: M' rs=raw_input('press any key to continue...')
; S. I5 M+ `+ N) T5 {7 e
# C# i0 v9 q3 {) {3 d
$ c8 }% b+ c, A' p; D# H
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2026-4-22 12:53 , Processed in 0.453435 second(s), 51 queries .

回顶部