简体   繁体   中英

Large python JSON post request times out

I'm trying to post some data using python, essentially a very large JSON file (around 420kB). The problem is that it works fine when it's posting to a local server, but times out for any external server. The code looks like this:

headers = {
"Content-Type": "application/json",
"Accept": "application/json"
};

response, content = httplib2.Http().request( 
    uri="http://%s:%s/json" % (host, port), method='POST',
    headers=headers, 
    body=json_string )

and the error I get is something along the lines of:

socket.error: [Errno 60] Operation timed out

Any thoughts on this? I was thinking that maybe the socket connection locally was timing out, but I have no idea how to fix this. All of the socket calls from the "import socket" library doesn't seem to have an effect.

UPDATE

I've tried it on a couple of different networks now, and it seems that it is a timeout issue, since the script works on all other networks besides a relatively slow one I'm on now. So the choice becomes either somehow stream the data or figure out a way to effectively increase the timeout.

It seems that the time required for the response is greater than the default timeout. Try giving the timeout in the

response, content = httplib2.Http().request( 
uri="http://%s:%s/json" % (host, port), method='POST',
headers=headers, 
body=json_string , timeout=100 ) (set the time as required)

or u can set a socket timeout large enough to bring the response

import socket
timeout = 100 (set the time as required)
socket. setdefaulttimeout(timeout)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM