create a mozHttpd package

RESOLVED FIXED in mozilla9

Status

RESOLVED FIXED
7 years ago
5 years ago

People

(Reporter: jmaher, Unassigned)

Tracking

unspecified
mozilla9
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(Whiteboard: [mozbase])

Attachments

(1 attachment)

(Reporter)

Description

7 years ago
Created attachment 561783 [details]
python file that works as a start to a mozhttpd package
Attachment #561783 - Flags: feedback?(jhammel)

Comment 1

7 years ago
Comment on attachment 561783 [details]
python file that works as a start to a mozhttpd package

 #literal #!/usr/bin/python

Please use #!/usr/bin/env python. None of this needs interpolation

> # ***** BEGIN LICENSE BLOCK *****
<snip type="boring"/>

> import SimpleHTTPServer
> import SocketServer
> import threading
> import sys
> import os
> import urllib
> import re

> #TODO: I don't like global variables, but is there a better way to do this?
> DOCROOT = '.'

I would put this as a class variable on mozHttpd and maybe have
MozRequestHandler live there too, but its really not important.

> class EasyServer(SocketServer.TCPServer):
>     allow_reuse_address = True
>
> class MozRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
>     def translate_path(self, path):
>         # It appears that the default path is '/' and os.path.join makes the '/'
>         return os.path.join(DOCROOT, path.strip('/'))

Probably *not* use os.path.join here, since for windows this will join
with '\'.

better: '/'.join[i.strip('/') for i in (DOCROOT, path)]

> class mozHttpd:

Please "subclass" from object until python 3: `mozHttpd(object)`

Also, capitalization: MozRequestHandler vs mozHttpd.  FWIW,
https://github.com/mozautomation/mozmill uses `Moz` for initial prefix

>     def __init__(self, host = "127.0.0.1", port = 8888, docroot =
      '.'):
Prefered style is no spaces except after ,s:

__init__(self, host="127.0.0.1", port=8888, ...

>         global DOCROOT
>         self.host = host
>         self.port = port
>         DOCROOT = docroot
>

One space between functions

>     def start(self):
>         self.httpd = EasyServer((self.host, self.port), MozRequestHandler)
>         self.server = threading.Thread(target=self.httpd.serve_forever)
>         self.server.setDaemon(True) # don't hang on exit
>         self.server.start()
>         self.testServer()
>
>     #TODO: figure this out
>     def testServer(self):
>         fileList = os.listdir(DOCROOT)
>
>         filehandle = urllib.urlopen('http://127.0.0.1:8888')

Use the self.data instead of hardcoding

>         data = filehandle.readlines();
>         filehandle.close()
>
>         for line in data:
>             found = False
>             # '@' denotes a symlink and we need to ignore it.
>             webline =
>         re.sub('\<[a-zA-Z0-9\-\_\.\=\"\'\/\\\%\!\@\#\$\^\&\*\(\)
>         ]*\>', '', line.strip('\n')).strip('/').strip().strip('@')

This....is awful :(

>             if (webline != ""):
>                 if (webline == "Directory listing for"):

Prefered python style is no ()s for if checks:

if webline != '': ...

>                     found = True
>                 else:
>                     for fileName in fileList:
>                         if fileName == webline:
>                             found = True
>
>                 if (found == False):
>                     print "NOT FOUND: " + webline.strip()
>
>     def stop(self):
>         self.httpd.shutdown()
>

Should probably do:

    __del__ == stop

Its actually not very good protection, but probably the best you can easily do
with python.

The logic and the structure seem like the right way to start.  Thanks for
doing this!
Attachment #561783 - Flags: feedback?(jhammel) → feedback+
(Reporter)

Comment 2

7 years ago
hmm, this seems to be slow, slow, slow.  I was trying talos tp4m and it fails miserably.
(Reporter)

Comment 3

7 years ago
we have this living on: https://github.com/mozilla/mozbase
Status: NEW → RESOLVED
Last Resolved: 7 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla9

Updated

5 years ago
Component: Infrastructure → General
You need to log in before you can comment on or make changes to this bug.