[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

BaseHTTPServer and do_POST method

Uwe Schmitt

2/27/2008 12:34:00 PM

Hi,

I am trying to implement a local server for storing and retrieving
numerical data.
So I use BaseHTTPServer as follows:

---------------------------------------------------------
from BaseHTTPServer import *

class Handler(BaseHTTPRequestHandler):

def do_POST(self):

print "POST"
self.send_response(200)


httpd = HTTPServer(("",8000), Handler)
httpd.serve_forever()
---------------------------------------------------------

For testing I use:

---------------------------------------------------------

import httplib


data = "123456789o" * 100

conn = httplib.HTTPConnection("localhost:8000")
print conn.request("POST", "/", data)

---------------------------------------------------------------

Executing this client, the server says:

error(10053, 'Software caused connection abort')

If I add "conn.getresponse()" at the end of the test script, the
message disapears, but the server hangs.

Where is my mistake ?

Greetings, Uwe.
4 Answers

7stud --

2/27/2008 1:51:00 PM

0

On Feb 27, 5:33 am, rocksportrocker <rocksportroc...@googlemail.com>
wrote:
> Hi,
>
> I am trying to implement a local server for storing and retrieving
> numerical data.
> So I use BaseHTTPServer as follows:
>
> ---------------------------------------------------------
>     from BaseHTTPServer import *
>
>     class Handler(BaseHTTPRequestHandler):
>
>         def do_POST(self):
>
>             print "POST"
>             self.send_response(200)
>
>     httpd = HTTPServer(("",8000), Handler)
>     httpd.serve_forever()
> ---------------------------------------------------------
>
> For testing I use:
>
> ---------------------------------------------------------
>
>     import httplib
>
>     data = "123456789o" * 100
>
>     conn = httplib.HTTPConnection("localhost:8000")
>     print conn.request("POST", "/", data)
>
> ---------------------------------------------------------------
>
> Executing this client, the server says:
>
>     error(10053, 'Software caused connection abort')
>
> If I add "conn.getresponse()" at the end of the test script, the
> message disapears, but the server hangs.
>
> Where is my mistake ?
>
> Greetings, Uwe.

I don't get that error. On the server, I get the output:

POST
localhost - - [27/Feb/2008 06:49:13] "POST / HTTP/1.1" 200 -

and on the client I get:

None

I don't know what's causing the second line of output on the server.

Uwe Schmitt

2/27/2008 2:39:00 PM

0

On Feb 27, 2:50 pm, 7stud <bbxx789_0...@yahoo.com> wrote:

> ...

> I don't get that error. On the server, I get the output:
>
> POST
> localhost - - [27/Feb/2008 06:49:13] "POST / HTTP/1.1" 200 -
>
> and on the client I get:
>
> None
>
In my case the server says:

----
Traceback (most recent call last):
File "c:\Python25\lib\SocketServer.py", line 222, in handle_request
self.process_request(request, client_address)
File "c:\Python25\lib\SocketServer.py", line 241, in process_request
self.finish_request(request, client_address)
File "c:\Python25\lib\SocketServer.py", line 254, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "c:\Python25\lib\SocketServer.py", line 522, in __init__
self.handle()
File "c:\Python25\lib\BaseHTTPServer.py", line 316, in handle
self.handle_one_request()
File "c:\Python25\lib\BaseHTTPServer.py", line 310, in
handle_one_request
method()
File "dataserver.py", line 11, in do_POST
self.send_response(200)
File "c:\Python25\lib\BaseHTTPServer.py", line 370, in send_response
self.send_header('Server', self.version_string())
File "c:\Python25\lib\BaseHTTPServer.py", line 376, in send_header
self.wfile.write("%s: %s\r\n" % (keyword, value))
File "c:\Python25\lib\socket.py", line 262, in write
self.flush()
File "c:\Python25\lib\socket.py", line 249, in flush
self._sock.sendall(buffer)
error: (10053, 'Software caused connection abort')
---

Greetings, Uwe




Uwe Schmitt

2/27/2008 2:58:00 PM

0


If I ommit send_response() in do_POST() I get no
errormessage. That is an acceptable solution for me.

Greetings, Uwe

Gabriel Genellina

2/27/2008 7:24:00 PM

0

En Wed, 27 Feb 2008 10:33:35 -0200, rocksportrocker
<rocksportrocker@googlemail.com> escribi�:

> Hi,
>
> I am trying to implement a local server for storing and retrieving
> numerical data.
> So I use BaseHTTPServer as follows:
>
> ---------------------------------------------------------
> from BaseHTTPServer import *
>
> class Handler(BaseHTTPRequestHandler):
>
> def do_POST(self):
>
> print "POST"
> self.send_response(200)
>
>
> httpd = HTTPServer(("",8000), Handler)
> httpd.serve_forever()
> ---------------------------------------------------------
>
> For testing I use:
>
> ---------------------------------------------------------
>
> import httplib
>
>
> data = "123456789o" * 100
>
> conn = httplib.HTTPConnection("localhost:8000")
> print conn.request("POST", "/", data)
>
> ---------------------------------------------------------------
>
> Executing this client, the server says:
>
> error(10053, 'Software caused connection abort')
>
> If I add "conn.getresponse()" at the end of the test script, the
> message disapears, but the server hangs.

If you don't add that line, the client process exits and drops the
connection, and the server detects that situation when it tries to send
the response to a dead socket.
The server "hangs" because you told it to "serve_forever"; it's waiting
for the next request. Use Ctrl-C to stop it.
You may find easier to use urllib or urllib2 to write the client.

--
Gabriel Genellina