473,614 Members | 2,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to read POSTed data

Hi.

I am trying to set up a simple HTTP-server but I have problems reading
data that is beeing POSTed.

class httpServer(Base HTTPServer.Base HTTPRequestHand ler):
def do_POST(self):
input = self.rfile.read ()

The self.rfile.read () will hang on the
data = self._sock.recv (recv_size)
line in the read() function in socket.py.

Is there another way to read the data?

Thanks,
Håkan Persson

Jul 18 '05 #1
15 13291

"Håkan Persson" <ha***@zyberit. com> wrote in message
news:ma******** *************** *************** *@python.org...
Hi.

I am trying to set up a simple HTTP-server but I have problems reading
data that is beeing POSTed.

class httpServer(Base HTTPServer.Base HTTPRequestHand ler):
def do_POST(self):
input = self.rfile.read ()

The self.rfile.read () will hang on the
data = self._sock.recv (recv_size)
line in the read() function in socket.py.

Is there another way to read the data?


I'm not an expert on this subject, but I am in the process of looking at the
code in BaseHTTPServer and other related modules. What I see used is
normally self.rfile.read line() and that makes more sense to me (intuitively)
than read(). Have you tried that?
Jul 18 '05 #2
I am piggybacking on Hakan's original posting because I am addressing the
same group of people (those with good knowledge in the standard web
programming modules), on a related topic. However, my question is
independent of Hakan's.

I have trouble getting a simple CGI script to work because it gets an empty
cgi.FieldStorag e form. I have looked into and tried to debug the internals
of the standard modules that are used but I cannot figure it out: how is a
multipart POST request parsed by CGIHTTPServer? BTW, I am using pyton 2.4.

I found the parsing of the header in the rfc822 module used by
BaseHTTPServer. BaseHTTPRequest Handler (through the mimetools module), but
that stops after the header (at the first empty line). Where is the parsing
done for the POST data following the header? I will appreciate any pointers
that will help me debug this problem.

As a side note, I found other old reports of problems with cgi handling POST
requests, reports that don't seem to have had a resolution. There is even a
bug reported just a few days ago (1112856) that is exactly about multipart
post requests. If I understand the bug report correctly though, it is only
on the latest version in CVS and it states that what is in the 2.4 release
works. All this tells me that it could be a "fragile" part in the standard
library. So it could be even a bug in the standard library, but for now I
am assuming that I'm doing something wrong.

Thanks,

Dan
Jul 18 '05 #3
Dan Perl wrote:
how is a multipart POST request parsed by CGIHTTPServer?
It isn't; the input stream containing the multipart/form-data content
is passed to the CGI script, which can choose to parse it or not using
any code it has to hand - which could be the 'cgi' module, but not
necessarily.
Where is the parsing done for the POST data following the header?
If you are using the 'cgi' module, then cgi.parse_multi part.
As a side note, I found other old reports of problems with cgi
handling POST requests, reports that don't seem to have had a
resolution.
(in particular?)

FWIW, for interface-style and multipart-POST-file-upload-speed reasons
I wrote an alternative to cgi, form.py
(http://www.doxdesk.com/software/py/form.html). But I haven't found
cgi's POST reading to be buggy in general.
There is even a bug reported just a few days ago (1112856) that is
exactly about multipart post requests. If I understand the bug
report correctly though, it is only on the latest version in CVS
and it states that what is in the 2.4 release works.
That's correct.
All this tells me that it could be a "fragile" part in the standard
library.


I don't really think so; it's really an old stable part of the library
that is a bit crufty in places due to age. The patch that caused
1112856 was an attempt to rip out and replace the parser stuff, which
as a big change to old code is bound to cause trouble. But that's what
the dev cycle is for.

CGIHTTPServer, on the other hand, I have never really trusted. I would
suspect that fella.

--
Andrew Clover
mailto:an*@doxd esk.com
http://www.doxdesk.com/

Jul 18 '05 #4
Dan Perl wrote:
I am piggybacking on Hakan's original posting because I am addressing the same group of people (those with good knowledge in the standard web
programming modules), on a related topic. However, my question is
independent of Hakan's.

I have trouble getting a simple CGI script to work because it gets an empty cgi.FieldStorag e form. I have looked into and tried to debug the internals of the standard modules that are used but I cannot figure it out: how is a multipart POST request parsed by CGIHTTPServer? BTW, I am using pyton 2.4.
I found the parsing of the header in the rfc822 module used by
BaseHTTPServer. BaseHTTPRequest Handler (through the mimetools module), but that stops after the header (at the first empty line). Where is the parsing done for the POST data following the header? I will appreciate any pointers that will help me debug this problem.

As a side note, I found other old reports of problems with cgi handling POST requests, reports that don't seem to have had a resolution. There is even a bug reported just a few days ago (1112856) that is exactly about multipart post requests. If I understand the bug report correctly though, it is only on the latest version in CVS and it states that what is in the 2.4 release works. All this tells me that it could be a "fragile" part in the standard library. So it could be even a bug in the standard library, but for now I am assuming that I'm doing something wrong.

Thanks,

Dan


Dan,
I was wondering how you were coming with your project.
I had wondered if i had missed something going the CherryPy route
instead of CGI. Now I see that you have had a bit of a snag , sorry to
hear that.
I am glad you are at least learning new things, 'cause if you had used
CherryPy2 you would have be done by now :P
M.E.Farmer

Jul 18 '05 #5

"M.E.Farmer " <me*****@hotmai l.com> wrote in message
news:11******** *************@l 41g2000cwc.goog legroups.com...
Dan,
I was wondering how you were coming with your project.
I had wondered if i had missed something going the CherryPy route
instead of CGI. Now I see that you have had a bit of a snag , sorry to
hear that.
I am glad you are at least learning new things, 'cause if you had used
CherryPy2 you would have be done by now :P
M.E.Farmer


I chose to go first through the cgi module and later CherryPy exactly
because I thought I will learn more this way. And I guess I am learning
more.
Jul 18 '05 #6

<an********@dox desk.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Dan Perl wrote:
how is a multipart POST request parsed by CGIHTTPServer?
It isn't; the input stream containing the multipart/form-data content
is passed to the CGI script, which can choose to parse it or not using
any code it has to hand - which could be the 'cgi' module, but not
necessarily.
Where is the parsing done for the POST data following the header?


If you are using the 'cgi' module, then cgi.parse_multi part.


Thanks, at least I was right not to find that in the CGIHTTPServer and
BaseHTTPServer code. So I have to use that instead of FieldStorage? I was
expecting FieldStorage to encapsulate all that for all the cases, POST with
multipart/form-data being just a special case.

I took a brief look at cgi.parse_multi part and I still have to figure out
how to provide the fp argument. Any code examples anywhere? All the
examples I have found were using only FieldStorage:
http://www.cs.virginia.edu/~lab2q/lesson_7/
http://www.devshed.com/index2.php?op...ge=0&hide_js=1
http://gnosis.cx/publish/programming...in_python.html
http://mail.python.org/pipermail/edu...ne/001368.html

BTW, I also tried the example in the last link and that doesn't work either,
with similar results/problems as my script. I think all those examples are
a few years old, has something changed since then?
As a side note, I found other old reports of problems with cgi
handling POST requests, reports that don't seem to have had a
resolution.


(in particular?)


I made the comment and now I have to back that up:
http://mail.python.org/pipermail/pyt...ead.html#88686
http://mail.python.org/pipermail/pyt...ad.html#124109
FWIW, for interface-style and multipart-POST-file-upload-speed reasons
I wrote an alternative to cgi, form.py
(http://www.doxdesk.com/software/py/form.html). But I haven't found
cgi's POST reading to be buggy in general.


I was quite careful in calling the code "fragile" and I am not normally the
kind of person to mince words. I would have said buggy if I meant that or I
could have used even worse words. But even the rationale behind the patch
that caused the bug I mentioned ("Remove dependencies on (deprecated) rfc822
and mimetools modules, replacing with email.") and even your calling this
part of the standard library "a bit crufty in places due to age" support my
view that this code needs work.

Besides, I am not happy at all with the idea of having to use
cgi.parse_multi part instead of FieldStorage. It seems a lot more low level
than I would like to even if it offers more control. I looked at your
form.py and you seem to address that. Sorry though, I will probably not use
it. Once I learn enough from the cgi module I will just move on to using a
framework like CherryPy.

Dan
Jul 18 '05 #7
Here is an example of how to get the POST data :

# def do_POST(self):
# ctype, pdict =
cgi.parse_heade r(self.headers. getheader('cont ent-type'))
# length = int(self.header s.getheader('co ntent-length'))
# if ctype == 'multipart/form-data':
# self.body = cgi.parse_multi part(self.rfile , pdict)
# elif ctype == 'application/x-www-form-urlencoded':
# qs = self.rfile.read (length)
# self.body = cgi.parse_qs(qs , keep_blank_valu es=1)
# else:
# self.body = {} # Unknown content-type
# # throw away additional data [see bug #427345]
# while select.select([self.rfile._soc k], [], [], 0)[0]:
# if not self.rfile._soc k.recv(1):
# break
# self.handle_dat a()

where handle_data() is the method where you will process the data received

The part related to bug #427345 is copied from CGIHTTPServer

For an example of use you can take a look at the CustomHTTPServe r in
Karrigell (http://karrigell.sourceforge.net)

A+,
Pierre
Jul 18 '05 #8

<an********@dox desk.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Dan Perl wrote:
how is a multipart POST request parsed by CGIHTTPServer?

[...]
CGIHTTPServer, on the other hand, I have never really trusted. I would
suspect that fella.


It turns out I was wrong thinking that the POST requests I was handling were
multipart. Blame that on my limited knowledge of HTTP. At least the
content-type header in the request doesn't say that they are multipart. I
checked with the an Ethereal sniffer and my Firefox browser on Windows sends
a request with 'content-type: application/x-www-form-urlencoded' and the
form data in the body of the request, after the headers. I'll assume for
now that this is a valid HTTP POST request as I believe that Firefox is
responsible for it alone and that it is not determined by my HTML form.

Pierre Quentel's suggestion for an implementation of do_POST in the "How to
read POSTed data" seems to handle requests of this kind, although I didn't
personally try it. But the run_cgi method in CGIHTTPServer expects the form
data to be only in the POST header line, in the path of the URL, like in a
GET request. I cannot find form data being parsed any other way if the
content-type is x-www-form-urlencoded. Am I missing something? Also, I
don't know if this means anything, but shouldn't CGIHTTPServer use the cgi
module if cgi has all those useful parsing utilities?

Dan
Jul 18 '05 #9

"Pierre Quentel" <qu************ @wanadoo.fr> wrote in message
news:42******** **************@ news.wanadoo.fr ...
Here is an example of how to get the POST data :

# def do_POST(self):
# ctype, pdict =
cgi.parse_heade r(self.headers. getheader('cont ent-type'))
# length = int(self.header s.getheader('co ntent-length'))
# if ctype == 'multipart/form-data':
# self.body = cgi.parse_multi part(self.rfile , pdict)
# elif ctype == 'application/x-www-form-urlencoded':
# qs = self.rfile.read (length)
# self.body = cgi.parse_qs(qs , keep_blank_valu es=1)
# else:
# self.body = {} # Unknown content-type
# # throw away additional data [see bug #427345]
# while select.select([self.rfile._soc k], [], [], 0)[0]:
# if not self.rfile._soc k.recv(1):
# break
# self.handle_dat a()

where handle_data() is the method where you will process the data received

The part related to bug #427345 is copied from CGIHTTPServer

For an example of use you can take a look at the CustomHTTPServe r in
Karrigell (http://karrigell.sourceforge.net)


Pierre, I am repeating some questions I already stated in another thread,
'CGI POST problem', but do you have any opinions on how CGIHTTPServer's
do_POST handles requests? It looks to me like it always expects form data
to be part of the POST command header, in the path of the URL, just like a
GET request. Am I understanding the code incorrectly? It would also make
sense to me that CGIHTTPServer should use the cgi module like you do in your
example, but it doesn't use cgi. Any opinions on that? Is there a history
there? I don't know enough about HTTP, especially about its history, but
was this a change in the HTTP specification at some point?

Thanks,

Dan
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
43018
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number (or would HDD be better, or both?) and put that info into VB prog so the program won't work on another computer. My program uses an MSAccess table. Much appreciated if you can help! Thanks
4
40246
by: francis70 | last post by:
Hi, I have these 2 problem? Is there a way in Oracle to read UNCOMMITED data. i.e. in Oracle the normal behaviour is that a user's updates to a table are visible to other users ONLY when the user commits. But in Informix there is this thing called ISOLATION LEVELS. For example by setting the ISOLATION LEVEL to DIRTY READ, a user will read dirty data, i.e. the last uncommited updated value of a field by some other user. Is
29
5794
by: pb648174 | last post by:
I have a very long transaction that runs on the same database that other users need to use for existing data. I don't care if they see data from the transaction before it is done and am only using the transaction because I need a way to roll it back if any errors happen during the transaction. Unfortunately all tables affected in the long running transaction are completely locked and nobody else can access any of the affected tables while...
9
1968
by: ferbar | last post by:
Hi all, I'm trying to read from the txt file 'ip.packets.2.txt' using the read function. It seems everything ok, but I get a -1 when executing >>bytesr = read(fdo1, bufread, 2); The 'open' function returns the file dsc 3. So this seems ok.. Any idea what might be wrong?
1
4130
by: Maksim Kasimov | last post by:
Hi, I'm trying to write an server-application, using BaseHTTPServer/BaseHTTPRequestHandler. When application is running, some client-application can post data to the server, and BaseHTTPRequestHandler reads all headers and posted raw data from "rfile", sends back response, than client/server session, than session closed - that works just fine. how to check whether some data posted again by the client in the session, and send response...
3
2047
by: Stephen | last post by:
Hi, I have a table consists which cosists of batch numbers, and assosiated dates and times..ie the columns are batch, date and time. The data within the table is not in any particular order. With a report, I need to calculate and display the difference in date / times between like batch numbers, ie displaying batch number, start date & time, finish date & time and duration
3
4317
by: Udhay | last post by:
Sir, I want to read the data of an audio file in c++ (Windows). What is the API which helps to read the data of an audio file. I want to read BitRate,Audio Sample Size,Audio Sample rate and Channel udhay
6
5699
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a * vector<string>. Now, use istringstream to read read each line * from the vector a word at a time.
4
2788
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have following data.bin (3 Data appended to 2 Data): 2, data0, data1, 3, data0, data1, data2
0
8198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8142
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8591
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8294
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8444
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6093
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4058
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2575
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.