question about python

fishfin calebjhansen at gmail.com
Sat Sep 13 04:00:59 EDT 2008


@ Carl: Yes, I think your right now that I look at it (or at least all
except for the last two lines need to be indented). I'm still not sure
how to send the stuff to the web browser though. Thanks for pointing
it out!

@ Diez: I'll start googling those right away.

Carl Banks wrote:
> On Sep 13, 12:15 am, fishfin <calebjhan... at gmail.com> wrote:
> > I was working through a tutorial about how to write a server using
> > python (the url is bellow). I am sure that the server is working to
> > some degree because when the server is running localhost:8080 just
> > keeps trying to load until it times out. I would like to know how to
> > send information through the server to my browser?
> >
> > I was working through this tutorial:http://python.about.com/od/networkingwithpython/ss/PythonWebServer.htm
> >
> > and my final code is like this:
> >
> > import socket
> >
> > host = ''
> > port = 8080
> >
> > c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> >
> > c.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
> >
> > c.bind((host, port))
> >
> > c.listen(1)
> >
> > while 1:
> >     csock, caddr = c.accept()
> >     cfile = csock.makefile('rw', 0)
>
> It looks like everything after this line needs to be indented.
> Besides that, nothing jumps out at me, though I don't do direct socket
> programming a lot.
>
> > line = cfile.readline().strip()
> >
> > cfile.write('HTTP/1.0 200 OK\n\n')
> > cfile.write('<html><head><title>Welcome %s!</title></head>' %
> > (str(caddr)))
> > cfile.write('<body><h1>Follow the link...</h1>')
> > cfile.write('All the server needs to do is ')
> > cfile.write('to deliver the text to the socket. ')
> > cfile.write('It delivers the HTML code for a link, ')
> > cfile.write('and the web browser converts it. <br><br><br><br>')
> > cfile.write('<font size="7"><center> <a href="http://python.about.com/
> > index.html">Click me!</a> </center></font>')
> > cfile.write('<br><br>The wording of your request was: "%s"' %(line))
> > cfile.write('</body></html>')
> >
> > cfile.close()
> > csock.close()
>
>
> Carl Banks



More information about the Python-list mailing list