BaseHTTPServer and do_POST method

R

rocksportrocker

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.
 
7

7stud

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.
 
R

rocksportrocker

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')
 
R

rocksportrocker

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

Greetings, Uwe
 
G

Gabriel Genellina

En Wed, 27 Feb 2008 10:33:35 -0200, rocksportrocker
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top