async def async_socket(req): # Create the socket using Pythons built-in socket class. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Set it to nonblocking. sock.setblocking(False) # Create a kore.socket with kore.socket_wrap(). conn = kore.socket_wrap(sock) # Asynchronously connect to 127.0.0.1 port 8888 await conn.connect("127.0.0.1", 8888) kore.log(kore.LOG_INFO, "connected!") # Now send the GET request msg = "GET /socket-test HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n" await conn.send(msg.encode()) kore.log(kore.LOG_INFO, "request sent!") # Read the response. data = await conn.recv(8192) kore.log(kore.LOG_INFO, "got response!") # Respond with the response from /socket-test. req.response(200, data) conn.close()
async def async_socket(req): # Create the socket using Pythons built-in socket class. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Set it to nonblocking. sock.setblocking(False) # Create a kore.socket with kore.socket_wrap(). conn = kore.socket_wrap(sock) # Asynchronously connect to 127.0.0.1 port 8888 await conn.connect("127.0.0.1", 8888) kore.log(kore.LOG_INFO, "connected!") # Now send the GET request msg = "GET /socket-test HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n" await conn.send(msg.encode()) kore.log(kore.LOG_INFO, "request sent!") # Read the response. data = await conn.recv(8192) kore.log(kore.LOG_INFO, "got response!") # Respond with the response from /socket-test. req.response(200, data) # Close the underlying socket, no need to close the wrapped kore.socket sock.close()
def __init__(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setblocking(False) sock.bind(("127.0.0.1", 6969)) sock.listen() self.conn = kore.socket_wrap(sock)
def __init__(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setblocking(False) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(("127.0.0.1", 6969)) sock.listen() self.conn = kore.socket_wrap(sock)