接口测试遇到tcp socket server的测试,需要模拟socket client,发送各种格式包,接受server返回值验证正确性。
tcp socket server例子:http://blog.csdn.net/qq_37023538/article/details/53957308
socket client代码
import socket
class TcpComm:
def __init__(self, _ip , _port):
self.send_addr = (_ip, _port)
self.buffersize = 10240000
self.comm_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.comm_socket.connect(self.send_addr)
self.recv_from_addr=()
def send(self,string_data):
self.comm_socket.send(string_data)
def recv(self):
self.string_data = self.comm_socket.recv(self.buffersize)
return self.string_data
def __del__(self):
self.comm_socket.close()
self.client_socket.close()
_tcp_client = TcpComm("127.0.0.1",8866)
_tcp_client.send("client_hello_world")
string_data = _tcp_client.recv()
print "recv from:", string_data