开发者

HTTP telnet POST/GAE server question (SIMPLE STUFF)

开发者 https://www.devze.com 2023-03-17 22:29 出处:网络
I am playing with HTTP transfers, just trying to make something work. I have a GAE server and I\'m pretty sure it\'s working properly because it renders when I go to it with my browser, but here is th

I am playing with HTTP transfers, just trying to make something work. I have a GAE server and I'm pretty sure it's working properly because it renders when I go to it with my browser, but here is the python code anyway:

import sys
print 'Content-Type: text/html'
print ''
print '<pre>'
number = -1
data = sys.stdin.read()
try:
    number = int(data[data.find('=')+1:])
except:
    number = -1
print 'If your number was', number, ', then you are awesome!!!'
print '</pre>'

I am just learning the whole HTTP POST vs GET vs Response process, but this is what I have been doing from the terminal:

$ telnet localhost 8080
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET http://localhost:8080/?number=28 HTTP/1.0

HTTP/1.0 200 Good to go
Server: Development/1.0
Date: Thu, 07 Jul 2011 21:29:28 GMT
Content-Type: text/html
Cache-Control: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Content-Length: 61

<pre>
If your number was -1 , then you are awesome!!!
</pre>
Connection closed开发者_JAVA技巧 by foreign host.

I am using a GET here because I stumbled around for about 40 minutes trying to make a telnet POST work - with no success :(

I would appreciate any help on how to get this GET and/or the POST to work. Thanks in advance!!!!


when using GET, no data will be present in the request body, so sys.stdin.read() is bound to fail. instead, you might want to look at the environment, specifically os.environ['QUERY_STRING']

Another thing you're doing a little strangely is you are not using the correct request format. The second part of the request should not include the url scheme, host or port, it should look like:

GET /?number=28 HTTP/1.0

specify the host in a seperate Host: header; the server will determine the scheme on it's own.

When using POST, most servers won't read past the amount of data in the Content-Length header, which if you don't supply one, may be assumed to be zero bytes. The server may try to read any bytes after the point specified by the content-length to be the next request in a persistent connection, and when it doesn't begin with a valid request, it closes the connection. So basically:

POST / HTTP/1.0
Host: localhost: 8080
Content-Length: 2
Content-Type: text/plain

28

But why are you testing this in telnet? How about curl?

$ curl -vs -d'28' -H'Content-Type: text/plain' http://localhost:8004/
* About to connect() to localhost port 8004 (#0)
*   Trying ::1... Connection refused
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 8004 (#0)
> POST / HTTP/1.1
> User-Agent: curl/7.20.1 (x86_64-redhat-linux-gnu) libcurl/7.20.1 NSS/3.12.6.2 zlib/1.2.3 libidn/1.16 libssh2/1.2.4
> Host: localhost:8004
> Accept: */*
> Content-Type: text/plain
> Content-Length: 2
> 
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Date: Thu, 07 Jul 2011 22:09:17 GMT
< Server: WSGIServer/0.1 Python/2.6.4
< Content-Type: text/html; charset=UTF-8
< Content-Length: 45
< 
* Closing connection #0
{'body': '28', 'method': 'POST', 'query': []}

or better yet, in python:

>>> import httplib
>>> headers = {"Content-type": "text/plain",
...            "Accept": "text/plain"}
>>> 
>>> conn = httplib.HTTPConnection("localhost:8004")
>>> conn.request("POST", "/", "28", headers)
>>> response = conn.getresponse()
>>> print response.read()
{'body': '28', 'method': 'POST', 'query': []}
>>> 
0

精彩评论

暂无评论...
验证码 换一张
取 消