jongpak / dev-tip

My Dev tips
1 stars 0 forks source link

request timeout #1

Open jongpak opened 5 years ago

jongpak commented 5 years ago

Server

import socket
import select
import time

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(('127.0.0.1', 1234))
server.listen(0)

print('listen...')

while True:
    read_socket_list, _, _ = select.select([server], [], [])

    for read_socket in read_socket_list:
        start = time.time()

        time.sleep(3)
        client, _ = read_socket.accept()
        print('accept => ' + str(time.time() - start))

        client.send(b'HTTP/1.0 200 OK\r\nContent-Length: 5\r\n\r\nSleep')
        time.sleep(5)

        client.close()
        print('close => ' + str(time.time() - start))

client

const request = require('request');

request('http://127.0.0.1:1234', { timeout: 4000 }, function(error, resp, body) {
    if (error) {
        console.error(error);
    } else {
        console.log(body);
    }
});