why this server can not run in python30

Discussion in 'Python' started by davy zhang, Oct 22, 2008.

  1. davy zhang

    davy zhang Guest

    import asyncore, asynchat
    import os, socket, string

    PORT = 8000

    class HTTPChannel(asynchat.async_chat):

    def __init__(self, server, sock, addr):
    asynchat.async_chat.__init__(self, sock)
    self.set_terminator("\r\n")
    self.request = None
    self.data = ""
    self.shutdown = 0

    def collect_incoming_data(self, data):
    self.data = self.data + data

    def found_terminator(self):
    if not self.request:
    # got the request line
    self.request = string.split(self.data, None, 2)
    if len(self.request) != 3:
    self.shutdown = 1
    else:
    self.push("HTTP/1.0 200 OK\r\n")
    self.push("Content-type: text/html\r\n")
    self.push("\r\n")
    self.data = self.data + "\r\n"
    self.set_terminator("\r\n\r\n") # look for end of headers
    else:
    # return payload.
    self.push("<html><body><pre>\r\n")
    self.push(self.data)
    self.push("</pre></body></html>\r\n")
    self.close_when_done()

    class HTTPServer(asyncore.dispatcher):

    def __init__(self, port):
    asyncore.dispatcher.__init__(self)
    self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
    self.bind(("", port))
    self.listen(5)

    def handle_accept(self):
    conn, addr = self.accept()
    HTTPChannel(self, conn, addr)

    #
    # try it out

    s = HTTPServer(PORT)
    print ("serving at port", PORT, "...")
    asyncore.loop()
     
    davy zhang, Oct 22, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. duncan
    Replies:
    2
    Views:
    6,095
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    909
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,993
    Smokey Grindel
    Dec 2, 2006
  4. Skybuck Flying
    Replies:
    16
    Views:
    679
    tragomaskhalos
    Aug 25, 2007
  5. tkinter in python30

    , Feb 17, 2009, in forum: Python
    Replies:
    2
    Views:
    291
    Chris Rebert
    Feb 17, 2009
Loading...

Share This Page