TCP echo 测试Python测试脚本
步骤:
1. 建立TCP/IP socket
2. 连接到正在侦听的服务器的端口上
3. 发送message“Happy123”
4. 接受信号
5. 对比数据
6. 关闭socket
import time
import threading
import os
import subprocess
import socket
import sys
time.sleep(5)
def interact_run(ip, port):
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = (ip, port)
print >>sys.stderr, 'connecting to %s port %s' % server_address
sock.connect(server_address)
try:
# Send data
message = 'Happy123'
print >>sys.stderr, 'sending "%s"' % message
for x in range(0, 5):
sock.sendall(message)
data = sock.recv(len(message))
if data != message:
break
print >>sys.stderr, 'received "%s"' % data
finally:
print >>sys.stderr, 'closing socket'
sock.close()
if data == message:
return 0,"Received=5"
else:
return 1,data
ret = interact_run('192.168.0.102', 7)
if ret[0]==0:
FREEMV_INTERACT_RESULT = 0
else:
FREEMV_INTERACT_RESULT = 1
FREEMV_ASSISTANT_CONTENT = 'following information was returned by echotool :'+ret[1]