User Controls

Basic HTTP Server.

  1. #1
    Sophie Pedophile Tech Support
    Ever need to serve some files locally for whatever reason? Well i do. So i made a script to do just that. I also added an option to invoke a quick OS shell in case you need to move or copy some files into the dir the files are being served from.

    I decided to post it here in case it may be useful to you. Dependencies are -> blessings, SimpleHTTPServer.


    #!/usr/bin/python 2.7

    import SimpleHTTPServer
    import SocketServer
    import time
    import os

    from blessings import Terminal

    t = Terminal()

    def quickshell():
    cwd = os.system('pwd')
    print "[" + t.green("+") + "]OS Shell in " + cwd
    print "[" + t.green("+") + "]Enter 'Q' to quit"

    try:

    while True:
    command = raw_input("\n<" + t.cyan("SERVER") + ">$ ")
    if not command in ('q', 'Q'):
    os.system(command)
    else:
    print "\n[" + t.red("!") + "]Exiting shell."
    time.sleep(1.5)
    break

    except KeyboardInterrupt:
    print "\n[" + t.red("!") + "]Critical. User Aborted"

    print "\n[" + t.green("+") + "]Basic HTTP Server.\n"

    default = raw_input("[" + t.magenta("?") + "]Default Config? [Y]es/[N]o: ")
    if default == 'y' or 'Y':

    PORT = 8000
    IP = "127.0.0.1"
    print "\n[" + t.green("+") + "]Default settings loaded.\n"

    elif default == 'n' or 'N':

    print "[" + t.green("+") + "]Specify custom values.\n"
    PORT = raw_input(int("[" + t.magenta("?") + "]Enter port: "))
    IP = raw_input("[" + t.magenta("?") + "]Enter host: ")

    print "[" + t.green("+") + "]Invoke a shell to make adjustments in server directory?"
    invoke = raw_input("[" + t.magenta("?") + "][Y]es/[N]o: ")
    if invoke == 'y' or 'Y':
    quickshell()
    elif invoke == 'n' or 'N':
    print "[" + t.green("+") + "]Done."
    else:
    print "\n[" + t.red("!") + "]Unhandled Option."

    else:
    print "\n[" + t.red("!") + "]Unhandled Option."


    print "[" + t.green("+") + "]Starting Server.\n"

    Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
    Handler.extensions_map.update({
    '.webapp': 'application/x-web-app-manifest+json',
    });

    try:
    httpd = SocketServer.TCPServer((IP, PORT), Handler)
    except Exception as e:
    print "\n[" + t.red("!") + "]Critical. An exception was raised with the following error message"
    print e

    print "[" + t.green("+") + "]Serving at", IP, repr(PORT)

    httpd.serve_forever()


    Post last edited by Sophie at 2017-12-26T03:57:23.945355+00:00
    The following users say it would be alright if the author of this post didn't die in a fire!
  2. #2
    Rivotril Houston
    Very nice code, it will help me with my Python studying.
  3. #3
    Sophie Pedophile Tech Support
    Originally posted by Rivotril Very nice code, it will help me with my Python studying.

    Thank you. I would have added comments but i wanted to write the script as fast as possible. I think the code is self-explanatory.
  4. #4
    Looks good. I'm not knowledgeable about Python, I thought everyone had upgraded to version 3 by now.
  5. #5
    Why are breaking changes in Python tolerated though? For instance the code for the internet prayer project just gives errors right now. Yes, it's from 2004, but Javascript from 2004 would still run. I can't even.
    http://www.zjulian.com/~adam/app/prayterm.html
  6. #6
    Sophie Pedophile Tech Support
    Originally posted by Issue313 Looks good. I'm not knowledgeable about Python, I thought everyone had upgraded to version 3 by now.

    Yes everyone except security people. Mostle because of legacy support on IoT devices and because 2.7 is just better.
  7. #7
    Lanny Bird of Courage
    Noice soph. One issue, I don't this this line is going to work:

    			if not command == 'q' or 'Q':


    The equality operator has a higher binding prescedence than the logical or operator. The way you'd read this line is "does command not equal 'q'? If not, then what is 'Q'?". You probably want "if not command in ('q', 'Q')`. The following code might make more sense than a written explanation:

    >>> 'foo' == 'bar' or 'foo'
    'foo'
    >>> 'baz' == 'bar' or 'foo'
    'foo'
    >>> 'foo' in ('bar', 'foo')
    True
    >>> 'baz' in ('bar', 'foo')
    False


    Also a useful tool I use pretty often is that you can run `python -m SimpleHTTPServer` to serve the current directory over HTTP. Same thing as you have here without the shell. It's nice since it's a oneliner and python is widely installed.

    Originally posted by Issue313 Looks good. I'm not knowledgeable about Python, I thought everyone had upgraded to version 3 by now.


    Probably over half of the community is on Py3 by now but it's been a very slow transition period. One of python3's big selling points was supposed to be better unicode support but a significant minority of the community, including myself, don't think py3's approach is a step in the right direction. There's a fair amount of nuance to the issue but basically py2's approach was "all strings are byte strings, unicode is complicated and you can use unicode strings if you want, but bytestrings are the default" which is a very reasonable strategy for a scripting/utility language. Py3 insists unicode codepoint sequences are common currency. That's nice if you're building a big system and you're willing to take the complexity hit of encoding and decoding at system boundaries. But it means if you just want to shuffle around a few bytes or treat your input as blackbox byte sequences you have to jump through hoops and work using second class language support.

    And if you're not sold on the unicode point there's next to nothing in py3 that moves forward from py2. Also all the cool lispy stuff got banished to functool siberia.

    Originally posted by Issue313 Why are breaking changes in Python tolerated though? For instance the code for the internet prayer project just gives errors right now. Yes, it's from 2004, but Javascript from 2004 would still run. I can't even.
    http://www.zjulian.com/~adam/app/prayterm.html
    asd

    Ehh, breaking changes in a major release isn't unreasonable. It's not hard to find a py2 interpreter. Java is a prime example of the terrible things that can happen to a language if you refuse to ever introduce breaking changes.

    Also lol, there would be a CDC sticker on that niggas monitor.
    The following users say it would be alright if the author of this post didn't die in a fire!
  8. #8
    SBTlauien African Astronaut
    Cool little Python server. I've already been using some really small C ones I found on Github.
  9. #9
    Sophie Pedophile Tech Support
    Originally posted by Lanny Noice soph. One issue, I don't this this line is going to work:

    			if not command == 'q' or 'Q':


    The equality operator has a higher binding prescedence than the logical or operator. The way you'd read this line is "does command not equal 'q'? If not, then what is 'Q'?". You probably want "if not command in ('q', 'Q')`. The following code might make more sense than a written explanation:

    >>> 'foo' == 'bar' or 'foo'
    'foo'
    >>> 'baz' == 'bar' or 'foo'
    'foo'
    >>> 'foo' in ('bar', 'foo')
    True
    >>> 'baz' in ('bar', 'foo')
    False

    You were right. The "not" operator doesn't work as intended on the offending line. Neither does "!=". I have updated the script.

    Post last edited by Sophie at 2017-12-26T04:05:04.434247+00:00
Jump to Top