[Twisted-Python] Custom webserver

Jp Calderone exarkun at intarweb.us
Thu Jun 19 20:15:58 EDT 2003


On Thu, Jun 19, 2003 at 11:27:55AM +0530, Vijay Rao wrote:
> At 06:34 16/06/2003 -0500, you wrote:
> 
> >Message: 9
> >Date: Mon, 16 Jun 2003 10:03:52 +0300
> >From: Tommi Virtanen <tv at twistedmatrix.com>
> >To: twisted-python at twistedmatrix.com
> >Subject: Re: [Twisted-Python] Custom webserver
> >Reply-To: twisted-python at twistedmatrix.com
> >
> >On Mon, Jun 16, 2003 at 09:57:13AM +0530, Vijay Rao wrote:
> >> I am trying to use twisted as a webserver that serves files only for
> >> requests from the localhost ie the desktop on which it is running by
> >> mapping localhost(127.0.0.1) to a domain pattern ( thru the hosts file on
> >> windows ) so that a particular type of URL is served from this webserver.
> >
> >        Easiest way is to make it bind to 127.0.0.1. There's currently
> >        no support in mktap web for this, though there really should
> >        be.
> >
> >--
> >:(){ :|:&};:
> 
> Thanks , any pointer as to how to achieve this binding.

  You can mktap --source or mktap --xml, then edit the resulting .tas or
.tax relatively easily.  The value to change will in tcpPorts and be '' or
"", representing "all interfaces".  Changing it to "127.0.0.1" will cause
the app to bind only to localhost when run.

  There certainly should be better support for this.

> Also for refusing any external request I tried this hack and it works :
> 
> (from protocols/http.py)
> 
>     def lineReceived(self, line):
>         if self.__first_line:
>             # if this connection is not persistent, drop any data which
>             # the client (illegally) sent after the last request.
> ......
> .....
> ......
>             if len(parts) != 3:
>                 self.transport.write("HTTP/1.1 400 Bad Request\r\n\r\n")
>                 self.transport.loseConnection()
>                 return
>             client = self.transport.getPeer()
>             host = self.transport.getHost()
>             if client[1] != '127.0.0.1' or host[1] != '127.0.0.1' :
>                 self.transport.write("HTTP/1.1 404 Forbidden\r\n\r\n")
>                 self.transport.loseConnection()
>                 return
> .......
> ....
> 
> Can this cause any other problem ( One thing I can see is that new channels 
> are created for each external request refused in this manner )
> And the requesting client browser is also not being able to display the 
> result sent.

  A better way would be to separate this accept/deny logic out into a
separate class, perhaps a Resource subclass.

  Both moshez and I have recently written code to support this.  Moshez's
version is by far the simpler of the two, and is available at
http://moshez.org/localonly.py.  My version uses a mini-language for
creating accept/deny rulesets, and is available in current Twisted CVS, in
sandbox/exarkun/rresource.py.

  Hope this helps,
 
  Jp

-- 
http://catandgirl.com/view.cgi?90
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20030619/4cf9163e/attachment.pgp 


More information about the Twisted-Python mailing list