1

I've a python socket server which listens to HTTP requests. It returns ip address and it's port after randomly choosing from a list of ip adresses. This result is generated by another file which fetches it from a database. The database is continuously updated. I want the list to be updated after every 10 requests or after 100 seconds any one of them will work. The below code doesn't work for me. The connection gets reset after every five requests. I printed the count and it increased to 10 after every 5 requests. Can anyone tell me what I'm missing here ?

result=get_ip() # Get a list of dictionary by calling get_ip() function
HOST, PORT = '127.0.0.1', 8890
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST, PORT))
listen_socket.listen(10)
print 'Serving HTTP on port %s ...' % PORT
count = 0
while True:
    if count % 10 == 0:
         result=get_ip()
    count +=1
    client_connection, client_address = listen_socket.accept()
    rand_ip = random.choice(result)
    ip = rand_ip["ip"]
    port = rand_ip["port"]
    client_connection.sendall(ip+":"+port)
    client_connection.close()
2
  • What does get_ip look like? Commented Nov 26, 2015 at 11:06
  • It's a normal mongodb connection that returns a JSON. Something like [{"ip":"127.0.0.1","port":"8080"},{....}]. Commented Nov 26, 2015 at 11:47

1 Answer 1

2

The problem comes from the close. It is dangerous to close a socket immediately after writing something into it. You should first shutdown the socket to make the peer to have a 0 read indicating a proper and of stream

So you script should end with:

client_connection.sendall(ip+":"+port)
try:
    client_connection.shutdown(socket.SHUT_WR)  # signal end of strem
    while True:  # wait for the client to close or shutdown his side
        q = client_connection.recv(1024)
        if len(q) == 0:
            break
finally:
    client_connection.close()  # close the socket only after the client has close its side
Sign up to request clarification or add additional context in comments.

3 Comments

Well this worked perfectly but I still didn't understand why the count increased twice after each request by the client ?
@Rahul: You need a tool like wireshark to spy what really happens between the HTTP client and the Python script. Maybe the client sent a second request when it found the connection abruptely closed?
Got it. Previously I was using chrome for sending request and found that it sends two requests. One as the normal GET request http://localhost:8888/ and another for http://localhost:8888/favicon.ico. It was giving the right result when I used curl for sending the request. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.