def test_echo(): n = network.Server() n.add_server(PORT, EchoServer) c = n.add_connection(('localhost', PORT), EchoClient) while c.is_open: # keep going until the client closes n.service() n.close()
def test_accept(): n = network.Server() n.add_server(PORT, AcceptServer) c = n.add_connection(('localhost', PORT), network.BasicHandler) # random connection while c.is_open: n.service() n.close()
def test_no_ssl_server(): n = network.Server() n.add_server(PORT, PlainServer) # non-ssl server c = n.add_connection(('localhost', PORT), Client, ssl=True) # ssl client while c.is_open: n.service() n.close() assert c.is_failed_handshake # ends badly
def test_success(): n = network.Server() n.add_server(PORT, network.BasicHandler, ssl=True, ssl_certfile='cert/cert.pem', ssl_keyfile='cert/key.pem') # self-signed certs c = n.add_connection(('localhost', PORT), SuccessClient, ssl=True) while c.is_open: n.service() n.close() assert c.is_failed_handshake is False # ssl handshake worked
def test_success(): n = network.Server() cert_dir = os.path.dirname(__file__) + '/cert/' certfile = cert_dir + 'cert.pem' keyfile = cert_dir + 'key.pem' n.add_server(PORT, network.BasicHandler, ssl=True, ssl_certfile=certfile, ssl_keyfile=keyfile) # self-signed certs c = n.add_connection(('localhost', PORT), SuccessClient, ssl=True) while c.is_open: n.service() n.close() assert c.is_failed_handshake is False # ssl handshake worked
print 'close: %s' % self.close_reason def on_ready(self): print 'sending' self.send(b'GET / HTTP/1.1\r\n\r\n') def on_send_complete(self): print 'send complete' def on_data(self, data): print 'new chunk of data:' print data print n = network.Server() c = n.add_connection(('www.google.com', 80), Google) # c = n.add_connection(('www.google.com', 443), Google, ssl=True) while True: ''' this is an easy way to figure out how to stop, but not very useful in real applications, for two reasons: 1. a painful delay is added at the end 2. a network delay encountered during connection or while sending or receiving data might cause a premature exit from the loop. since this is an HTTP connection, using an http.HTTPHandler would allow for more accurate determination of a complete network exchange.