Question...

Peter Hansen peter at engcorp.com
Tue Jul 31 20:39:46 EDT 2001


Adonis Vargas wrote:
> 
> how come this code does not seem to popup on any broswer i attempt to
> send HTML data to:
> 
>     (c, a) = s.accept()
>     c.send('HTTP 200 OK\n')
>     c.send('Content-Type: text/html\n')
>     c.send('')

As noted by Steven, c.send('') does nothing... so you aren't
sending the required extra newline here.

Why are you sending anything to the browser before receiving what
it has sent to you?  I think with HTTP you should send nothing
until you get the request from the client....

>     d = c.recv(256)

Why 256?  This seems to be your problem.  What if the client 
has more data to send before it is done with you?  It won't 
accept your response because you closed the socket before it 
was even finished talking to you.  I tried something based on
your basic code (although it wouldn't have hurt to post the
three lines required to set up the connection and removed the
RED and RESET stuff so you had a self-contained example),
and changing 256 to 2560 was enough to get a redirect...
(to www.engcorp.com at least, since www.python.org didn't
respond at the time :-( )

I hope you are doing this as a learning exercise, because if
you are actually trying to write an HTTP server from scratch
you should try using the standard library modules that already
implement HTTP in a much more thorough and correct manner.
Or at least they could serve as good examples/starting points.

Cheers!
-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list