Пример #1
0
def test_SendMultipleDataMultipleClient():
    a = Socket()
    b = Socket()
    c = Socket()
    d = Socket()
    e = Socket()
    a.listen()
    b.connect(a.port)
    c.connect(a.port)
    d.connect(a.port)
    e.connect(a.port)

    b.send(data[0])
    data_out = a.receive()
    assert data_out == data[0]

    c.send(data[1])
    data_out = a.receive()
    assert data_out == data[1]

    d.send(data[2])
    e.send(data[3])
    data_out1 = a.receive()
    data_out2 = a.receive()
    assert data_out1 == data[2]
    assert data_out2 == data[3]

    a.shutdown(); b.shutdown(); c.shutdown(); d.shutdown(); e.shutdown()
Пример #2
0
def test_disconnect():
    a = Socket()
    b = Socket()
    a.listen()
    b.connect(a.port)

    time.sleep(0.1)

    target = ('127.0.0.1', a.port)
    sock = b.sockets[target]
    data_in = 'disconnect test'

    data_serialized = b._prepareData(data_in, target)
    data_header = struct.pack('!IH', len(data_serialized), b.send_msg_idx[target])
    b.send_msg_idx[target] += 1

    sock.shutdown(2)
    sock.close()

    b.data_awaiting[target].append(data_header + data_serialized)

    data_out = a.receive()
    assert data_out == data_in

    a.shutdown(); b.shutdown()
Пример #3
0
def test_discover():
    a = Socket()
    b = Socket()
    a.listen()
    a.advertise("test_type", "test_name", "test_advertiser")
    time.sleep(0.5)
    res = b.discover("test_type", "test_name")
    print(res)
Пример #4
0
def example1():
    a, b = Socket(), Socket()

    a.listen() # Creating a server is done with .listen()
    b.connect(a.port) # Creating a client is done with .connect()

    b.send('An example.')
    data = a.receive()
    print("Data received:", data)
Пример #5
0
def test_ShutdownClientServer():
    a = Socket()
    b = Socket()
    a.listen(31415)
    b.connect(31415)

    b.shutdown()
    a.shutdown()

    assert threading.active_count() <= 1
Пример #6
0
def test_SendToSingleClient(data_in):
    a = Socket()
    b = Socket()
    a.listen()
    b.connect(a.port)

    a.send(data_in)
    data_out = b.receive()
    assert data_in == data_out

    a.shutdown(); b.shutdown()
Пример #7
0
def test_asynchronousConnect():
    a = Socket()
    b = Socket()

    a.listen()
    b.connect(a.port, timeout=0)

    data_in = "test data"
    b.send(data_in)
    data_out = a.receive()
    assert data_in == data_out
Пример #8
0
def test_SendToServer(data_in):
    a = Socket()
    b = Socket()
    a.listen()
    b.connect(a.port)

    b.send(data_in)
    data_out = a.receive()
    assert data_in == data_out

    b.shutdown(); a.shutdown()
Пример #9
0
def test_multipleTargets():
    raise NotImplementedError()
    a = Socket()
    b = Socket()
    c = Socket()
    a.listen()
    b.listen()
    c.connect(a.port)
    c.connect(b.port)

    c.send()
Пример #10
0
def test_compressionAndSecure(data_in):
    a = Socket(compress=True, encrypt=True)
    b = Socket(compress=True, encrypt=True)

    a.listen()
    b.connect(a.port)

    b.send(data_in)
    data_out = a.receive()
    assert data_out == data_in

    a.shutdown(); b.shutdown()
Пример #11
0
def test_Security(data_in):
    a = Socket(encrypt=True)
    b = Socket(encrypt=True)

    a.listen()
    b.connect(a.port)

    b.send(data_in)
    data_out = a.receive()
    assert data_out == data_in

    a.shutdown(); b.shutdown()
Пример #12
0
def test_ShutdownMultiple():
    a = Socket()
    b = Socket()
    c = Socket()
    a.listen(27182)
    b.connect(27182)
    c.connect(27182)

    c.shutdown()
    b.shutdown()
    a.shutdown()

    assert threading.active_count() <= 1
Пример #13
0
def test_multipleListen():
    a = Socket()
    b = Socket()

    a.listen(12340)
    a.listen(12341)
    b.connect(12340)
    b.connect(12341)

    data_in = "test data"
    b.send(data_in)
    data_out1, data_out2 = a.receive(), a.receive()
    assert data_in == data_out1
    assert data_in == data_out2
Пример #14
0
def test_SendMultipleData(data_in1, data_in2):
    a = Socket()
    b = Socket()
    a.listen()
    b.connect(a.port)

    b.send(data_in1)
    b.send(data_in2)

    data_out1, data_out2 = a.receive(), a.receive()

    assert data_in1 == data_out1
    assert data_in2 == data_out2

    a.shutdown(); b.shutdown()
Пример #15
0
def test_CreateTwoServersBackToBack():
    a = Socket()
    b = Socket()
    c = Socket()

    a.listen()
    a.shutdown()
    b.listen(a.port)
    c.connect(b.port)

    c.send(data[0])
    data_out = b.receive()
    assert data[0] == data_out

    a.shutdown(); b.shutdown(); c.shutdown()
Пример #16
0
def test_SendMultipleData():
    a = Socket()
    b = Socket()
    a.listen()
    b.connect(a.port)

    for y in range(10):
        for x in data:
            b.send(x)

    for y in range(10):
        for x in data:
            data_out = a.receive()
            assert x == data_out

    a.shutdown(); b.shutdown()
Пример #17
0
def test_SendToAllClients(data_in):
    # Testing broadcast

    a = Socket()
    b = Socket()
    c = Socket()

    a.listen()
    b.connect(a.port)
    c.connect(a.port)

    a.send(data_in)
    data_out1, data_out2 = b.receive(), c.receive()
    assert data_in == data_out1 == data_out2

    a.shutdown(); b.shutdown(); c.shutdown()
Пример #18
0
def example4():
    # Testing multiple connection sending by a client

    h = Socket()
    i = Socket()
    j = Socket()
    h.listen(16180)
    i.listen(12020)
    j.connect(16180)
    j.connect(12020)

    time.sleep(0.1)

    j.send(b'Patate')
    print(h.receive())
    print(i.receive())
Пример #19
0
def example3():
    # Testing broadcast

    e = Socket()
    f = Socket()
    g = Socket()

    e.listen(14142)
    f.connect(14142)
    g.connect(14142)

    time.sleep(0.1)

    e.send(b'broadcast')
    print(f.receive())
    print(g.receive())
Пример #20
0
def test_SendToAllServers(data_in):
    # Testing multiple connection sending by a client

    a = Socket()
    b = Socket()
    c = Socket()
    a.listen()
    b.listen()
    c.connect(a.port)
    c.connect(b.port)

    c.send(data_in)
    data_out1, data_out2 = a.receive(), b.receive()
    assert data_in == data_out1
    assert data_in == data_out2

    a.shutdown(); b.shutdown(); c.shutdown()
Пример #21
0
def example2():
    # Testing sending from the server

    c = Socket()
    d = Socket()

    # You can specify the port and listening address
    c.listen(27182, '0.0.0.0')
    d.connect(27182, '127.0.0.1')

    time.sleep(0.1) # Wait for the server to receive the connection from the client

    # You can send from the server to the client.
    # To send to a particular target, the tuple of (address, port) is required.
    d.send('Another example', ('127.0.0.1', 27182))
    data = c.receive()
    print(data)
Пример #22
0
def test_wronglyConfiguredSocket():
    a = Socket(compress=True)
    b = Socket(compress=False)
    a.listen()
    with pytest.raises(AssertionError):
        b.connect(a.port)