So I'm running the Google App Engine development server (Java) on localhost. I'm trying to retrieve a URL using Python 2.7 urllib.urlopen. The initial retrieve works, but then when I try to call read() or readlines() I get:
Traceback (most recent call last):
File "./getMap.py", line 6, in
lst = f.readlines()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 513, in readlines
line = self.readline()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 445, in readline
data = self._sock.recv(self._rbufsize)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 552, in read
s = self.fp.read(amt)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 378, in read
data = self._sock.recv(left)
socket.error: [Errno 54] Connection reset by peer
The browser works, wget works. Problem occurs with both urllib and urllib2. Here's the code:
f = urllib2.urlopen("http://localhost:8080/default.jsp")
lst = f.readlines()
for a in lst:
Strangely, I can print out the first line of the file using readline() -- I just can't get the *whole* file. I get the sense that maybe Python is "lazily" not requesting the entire contents of the URL until I request it via readlines(), and by then the app engine dev server has overzealously closed the connection. But I could be totally wrong about that.
I tried researching this problem but I didn't see anything applicable. Most of the Google hits I'm seeing surround random, intermittant timing issues (this isn't an intermittant problem, it's reliable) or proxy/firewall issues (nothing like that going on here).
Assuming my theory is correct -- is there a way to tell urlopen to get the whole response right away, as wget and the browser seem to be doing? Or is there a way to tell the GAE dev server to chill out and not close the connection so quickly? I'd rather not dive into lower-level python socket stuff if I don't have to.
p.s. clarification: the python script is just running from the command line and trying to make a connection to the GAE dev server, which is running on the same box. I'm NOT trying to connect to the GAE dev server from itself or something weird like that, the GAE server is running Java, not Python. What I'm actually trying to do here is this: my GAE web app has some web services and I'm writing a batch script to get/post to those webservices, so that when I need to reset/clear the data store (example: data gets corrupted) I can use this python script to back up the data first, then I erase the data store, and then use the script again to load that data back in.
UPDATE: so I tried a few more tests. Python has no trouble reading any HTML file served by the GAE dev server. However any JSP, even the simplest "hello world" JSP, fails to read with the same "connection reset by peer" error. I'll try updating to the 1.6.1 version of the GAE SDK, I have to do that anyway at some point, might as well be now. Hopefully it will fix this.
Does f.read() yield the whole file?
以上就是Python script won't read() after urlopen from localhost gae dev server -- Connection Reset By Peer的详细内容，更多请关注web前端其它相关文章！