如何使用python测试环回套接字连接?

时间:2013-10-21 06:37:57

标签: python sockets

我想测试一个复杂的类,它包含了socket模块的一些方法:connectsendallrecv。特别是,我想测试这个类的recv方法。

下面的工作示例代码展示了我如何做到这一点(在基本的基础形式中保持简单,testsocket将对应于复杂的包装类):

import socket

# This is just a socket for testing purposes, binds to the loopback device
sock =  socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("127.0.0.1", 1234))
sock.listen(5)

# This is the socket later part of the complex socket wrapper.
# It just contains calls to connect, sendall and recv
testsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
testsocket.connect(("127.0.0.1", 1234))
testsocket.sendall("test_send")

# The testing socket connects to a client
(client, adr) = sock.accept()
print client.recv(1024)

# Now I can do the actual test: Test the receive method of the socket
# wrapped in the complex class
client.sendall("test_recv")
print testsocket.recv(1024)  # <--  This is what I want to test !!

# close everything
testsocket.close()
client.close()
sock.close()

但是为了测试testsocket.recv,我之前需要使用testsocket.sendall

是否可以以简单的方式修改此代码(没有分支或线程),以便在不使用方法testsocket.recv的情况下测试testsocket.sendall

2 个答案:

答案 0 :(得分:1)

如何使用socket.socketpair? :

import socket

client, testsocket = socket.socketpair()
client.sendall("test_recv")
print testsocket.recv(1024)
testsocket.close()
client.close()

注意仅适用于Unix。


使用mock

import mock

testsocket = mock.Mock()
testsocket.configure_mock(**{'recv.return_value': 'test_recv'})
print testsocket.recv(1024)

答案 1 :(得分:0)

由于server.recv()是阻止调用,因此无法在同一进程/线程中运行客户端和服务器套接字

我的日常工作:

import socket, threading

# Protocols supported
TCP = (0x00)
UDP = (0x01)
UDP_Multicast = (0x02)

# Client/ Server mode
CLIENT = (0x00)
SERVER = (0x01)

# Data to be sent
data = 'Test. Please Ignore'


# Server processing
def simple_processing(data):
    print "messsage : ", data


def start_socket( protocol, client, processing_callback):

    # switch on protocol
    if protocol == TCP:
        sock =  socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    else: 
        return

    # switch on client
    if client == SERVER:

        # Server mode = listening to incoming datas
        sock.bind(("127.0.0.1", 1234))
        sock.listen(5)
        (sock, adr) = sock.accept()
        processing_callback( sock.recv(1024) ) # processing data

    elif client == CLIENT:

        # Client mode : connecting and sending data
        sock.connect(("127.0.0.1", 1234))
        sock.sendall(data)


    else:
        return

    sock.close()


def test():

    # Thread creations
    server = threading.Thread( target = start_socket,
                               args=(   TCP,
                                        SERVER,
                                        simple_processing, )  
                              )

    client = threading.Thread(  target= start_socket, 
                                args=(  TCP,
                                        CLIENT,
                                        None)
                                )

    server.start()
    client.start()


    # Join : wait on every thread to finish
    client.join()
    server.join()

if __name__ == '__main__':
    # Launch the test
    test()