question about python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fishfin

    question about python

    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:



    and my final code is like this:

    import socket

    host = ''
    port = 8080

    c = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)

    c.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR, 1)

    c.bind((host, port))

    c.listen(1)

    while 1:
    csock, caddr = c.accept()
    cfile = csock.makefile( 'rw', 0)

    line = cfile.readline( ).strip()

    cfile.write('HT TP/1.0 200 OK\n\n')
    cfile.write('<h tml><head><titl e>Welcome %s!</title></head>' %
    (str(caddr)))
    cfile.write('<b ody><h1>Follow the link...</h1>')
    cfile.write('Al l 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('an d the web browser converts it. <br><br><br><br >')
    cfile.write('<f ont size="7"><cente r<a href="http://python.about.co m/
    index.html">Cli ck me!</a</center></font>')
    cfile.write('<b r><br>The wording of your request was: "%s"' %(line))
    cfile.write('</body></html>')

    cfile.close()
    csock.close()
  • Diez B. Roggisch

    #2
    Re: question about python

    fishfin schrieb:
    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:

    >
    >
    and my final code is like this:
    >
    import socket
    >
    host = ''
    port = 8080
    >
    c = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    >
    c.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR, 1)
    >
    c.bind((host, port))
    >
    c.listen(1)
    >
    while 1:
    csock, caddr = c.accept()
    cfile = csock.makefile( 'rw', 0)
    >
    line = cfile.readline( ).strip()
    >
    cfile.write('HT TP/1.0 200 OK\n\n')
    cfile.write('<h tml><head><titl e>Welcome %s!</title></head>' %
    (str(caddr)))
    cfile.write('<b ody><h1>Follow the link...</h1>')
    cfile.write('Al l 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('an d the web browser converts it. <br><br><br><br >')
    cfile.write('<f ont size="7"><cente r<a href="http://python.about.co m/
    index.html">Cli ck me!</a</center></font>')
    cfile.write('<b r><br>The wording of your request was: "%s"' %(line))
    cfile.write('</body></html>')
    >
    cfile.close()
    csock.close()
    Do yourself a favor and look into the various python webframeworks such
    as TurboGears, Django and what not.

    Or at least into the module SimpleHTTPServe r.

    Diez

    Comment

    • Carl Banks

      #3
      Re: question about python

      On Sep 13, 12:15 am, fishfin <calebjhan...@g mail.comwrote:
      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/networkin...nWebServer.htm
      >
      and my final code is like this:
      >
      import socket
      >
      host = ''
      port = 8080
      >
      c = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
      >
      c.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR, 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('HT TP/1.0 200 OK\n\n')
      cfile.write('<h tml><head><titl e>Welcome %s!</title></head>' %
      (str(caddr)))
      cfile.write('<b ody><h1>Follow the link...</h1>')
      cfile.write('Al l 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('an d the web browser converts it. <br><br><br><br >')
      cfile.write('<f ont size="7"><cente r<a href="http://python.about.co m/
      index.html">Cli ck me!</a</center></font>')
      cfile.write('<b r><br>The wording of your request was: "%s"' %(line))
      cfile.write('</body></html>')
      >
      cfile.close()
      csock.close()

      Carl Banks

      Comment

      • fishfin

        #4
        Re: question about python

        @ 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...@g mail.comwrote:
        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/networkin...nWebServer.htm

        and my final code is like this:

        import socket

        host = ''
        port = 8080

        c = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)

        c.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR, 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('HT TP/1.0 200 OK\n\n')
        cfile.write('<h tml><head><titl e>Welcome %s!</title></head>' %
        (str(caddr)))
        cfile.write('<b ody><h1>Follow the link...</h1>')
        cfile.write('Al l 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('an d the web browser converts it. <br><br><br><br >')
        cfile.write('<f ont size="7"><cente r<a href="http://python.about.co m/
        index.html">Cli ck me!</a</center></font>')
        cfile.write('<b r><br>The wording of your request was: "%s"' %(line))
        cfile.write('</body></html>')

        cfile.close()
        csock.close()
        >
        >
        Carl Banks

        Comment

        • Carl Banks

          #5
          Re: question about python

          On Sep 13, 1:00 am, fishfin <calebjhan...@g mail.comwrote:
          @ 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!
          Try reading in the whole HTTP request instead of just the first line.
          Change

          line = cfile.readline( ).strip()

          to

          line = cfile.read()

          And see if that helps. (Outputting the document so that it formats
          the request well is left as an exercise. Also, as a heads up: in real
          programs you should never output anything you receive through the
          network without checking it or escaping it to prevent malicious uses.)


          Carl Banks

          Comment

          • fishfin

            #6
            Re: question about python

            On Sep 13, 4:25 pm, Carl Banks <pavlovevide... @gmail.comwrote :
            On Sep 13, 1:00 am, fishfin <calebjhan...@g mail.comwrote:
            >
            @ 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!
            >
            Try reading in the whole HTTP request instead of just the first line.
            Change
            >
            line = cfile.readline( ).strip()
            >
            to
            >
            line = cfile.read()
            >
            And see if that helps.  (Outputting the document so that it formats
            the request well is left as an exercise.  Also, as a heads up: in real
            programs you should never output anything you receive through the
            network without checking it or escaping it to prevent malicious uses.)
            >
            Carl Banks
            I figured out what the problem was. When you had suggested that I
            indent the lines at first I did all of them, but when I did that there
            must have been an nonindented line before the last two lines which I
            had indented, so ending the 'while 1:'. Because of that, that code
            just flat out didn't work so I assumed that they must be not be
            indented which is why it hasn't been working all along. Thanks for
            your help! I don't think I would have every figured it out if you last
            post hadn't gotten me to thinking about little tweeks like that.

            Comment

            Working...