我需要为两个独立的原始模块服务器和客户端编写网络连接的自动化测试。无法使用套接字服务器。 server.py的内容
import socket
def sniff_pack():
sock = socket.socket()
sock.bind(('', 9999))
sock.listen(1)
conn, addr = sock.accept()
print 'connected:', addr
while True:
data = conn.recv(1024)
if not data:
break
conn.send(data.upper())
conn.close()
if __name__ == '__main__':
sniff_pack()
client.py的内容
import socket
def send_pack():
adress = 'localhost'
port = 9999
sock = socket.socket()
sock.connect((adress, port))
sock.send('hello, world!')
data = sock.recv(1024)
sock.close()
return data
if __name__ == '__main__':
send_pack()
线程无法成功。 知道怎么跟随GIL打架吗?
from server import sniff_pack
from client import send_pack
import unittest
class PackTestCase(unittest.TestCase):
def setUp(self):
sniff_pack()
def test_proper_pack(self):
self.assertEqual(send_pack(),'HELLO, WORLD!')
if __name__ == '__main__':
unittest.main()