QQ登录

只需要一步,快速开始

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

tcp的socket连接报文测试工具

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

1199

主题

4

听众

2980

积分

该用户从未签到

跳转到指定楼层
1#
发表于 2024-3-31 17:21 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
# -*- coding: utf-8 -*-. y9 x& A. a8 f) R& H6 ~8 d7 I
import socket
5 ^9 Z( p- U1 ]from myutil import *
& @  z1 Z' S, R* L% Mfrom binascii import unhexlify as unhex
  N  v9 b" X- Ufrom ctypes import *
) ^! G- O% Z! \dll = cdll.LoadLibrary('mydll.dll')3 f9 w  y. {1 D
print 'begin load mydll..'
* C+ t9 M- Q# u& z) [  SHOST, PORT = "192.168.51.28", 5800
$ l6 {* H2 ?, ^! N$ E% Nsd ="1234567812345678": g4 G* Z# U2 {- b/ q) a: R
# Create a socket (SOCK_STREAM means a TCP socket)
5 s$ |: e) t" D: n! a9 ^sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM): X% @8 y; P: e! X+ M
try:
. v4 ?. x. v* F- l    # Connect to server and send data
+ u2 l3 m5 m1 u6 E/ l  L$ t! {    sock.connect((HOST, int(PORT))
0 |2 B: E) m% E2 ], V% _    print "Sent1 OK:"
2 o! M5 x. y# _0 c! J; _" E    print sd
; z6 W. D1 h& J8 M1 y& ^    # Receive data from the server and shut down
- B1 k: v( o5 Z7 D( G    received = sock.recv(1024): M' R' K, m: k/ v& `* j4 Y, I
    print "Received:"$ R5 u; g( V3 A. D2 z1 Y7 s3 [
    print_hex(received)
. n- O6 V5 L- d& X7 Q$ d2 @1 V7 E& y    print  'received len is 0x%02x' %(len(received))
* x' l  Y2 }1 |" x0 U  |    print  'received data analysis...'' T/ A6 l  h  k& T
    re1=received[0:4]  ^- R) Y: S4 d2 Z
    print_hex(re1)
  p7 O  y  R4 R& V    re1=received[4:6]; z$ p( U. g' @9 ]' y$ b3 R' H3 @
    print_hex(re1)7 L# }( H3 t( o. L% [2 `
    re1=received[6:10]
8 r8 M: Q. h- F4 C% {: O0 N5 q    print_hex(re1)
+ x4 A9 f' f, x* e! f7 y    re1=received[10:16]
4 t7 ?! E: k" b# G2 c9 @    print_hex(re1)& U* j" q' f8 L

2 N8 X# [4 d6 C6 Z+ z7 p; I    #pack2 send
9 \, U# u( d$ i$ B1 D% N    sock.send(sd2.decode('hex'))
, ~' m: ?6 g- `    print "Sent2 OK:"
, y0 Q+ E$ A: \( B0 a9 p    print sd2
7 j7 y5 ?7 J; K    # Receive data from the server and shut down8 u9 S/ b+ i7 _' Y! _1 @) j7 R
    received1 = sock.recv(1024)5 R$ w. A! m& A8 I
    print "Received1:"& F# F6 V0 s& L" k) ]% U
    print_hex(received1)3 L, V% u0 \4 G! A. }/ `8 v3 R
    print  'received1 len is 0x%02x' %(len(received1))
9 |. F- V) W' `" x  N- O. k: K2 k& X) R
finally:+ @: Q/ j( z% m9 F
    sock.close()+ v) z( G5 r8 E! t
4 y2 L7 s: U# [* M  z* r$ z
s=raw_input('press any key to continue...')
2 O3 x5 `8 x. d$ W; O" e- J
1 _1 @" ]: J1 M! R- k5 f
. C8 s- x% |6 e7 C. S" F) t
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

fastpost qq
收缩
  • 电话咨询

  • 04714969085

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

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

蒙公网安备 15010502000194号

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

GMT+8, 2026-12-6 02:04 , Processed in 2.512808 second(s), 51 queries .

回顶部