<reponame>tekktonic/programming<filename>wof/initserver.py
#!/usr/bin/env python
"""Copyright (c) 2012, <NAME>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the WOF nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <NAME> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."""
import os, sys, re
import socket as sock

s = sock.socket(sock.AF_INET, sock.SOCK_STREAM)
s.setsockopt(sock.SOL_SOCKET, sock.SO_REUSEADDR, 1)
s.bind(("192.168.155.105", 1337))

s.listen(1)

globaldead = False

while not globaldead:
    dead = False

    clientsock, addr = s.accept()

    string = clientsock.recv(1024)
    realstring = string.split()

    if len(realstring) < 1: # if we get an empty message, ignore it
        continue

    elif realstring[0] == "LIST":
        ret = ""
        if len(realstring) == 1: # if there's only LIST and no directory, assume .
            wanteddir = "."
        else:
            wanteddir = realstring[1]

        if os.path.exists(wanteddir):
            if os.path.isdir(wanteddir):
                for contents in os.listdir(wanteddir):
                    if os.path.isdir(os.path.join(wanteddir, contents)):
                        ret += ("D\t" + contents + "\r\n")
                    else:
                        ret += ("F\t" + contents + "\r\n")

                        clientsock.sendall(ret)
            else:
                clientsock.send("ERR_ISFILE\r\n") # we can't exactly list a file, can we?
        else:
            clientsock.send("ERR_NOEXIST\r\n")

    elif realstring[0] == "GET":
        if len(realstring) == 1:
            clientsock.send("ERR_NOARG\r\n")
        elif os.path.isdir(realstring[1]):
            clientsock.send("ERR_ISDIR\r\n")
        else:
            f = open(realstring[1], "r")
            data = f.read(4096)
            while data:
                clientsock.send(data)
                data = f.read(4096)

    elif realstring[0] == "SIZE":
        if len(realstring) == 1:
            clientsock.send("ERR_NOARG\r\n")
        else:
            if os.path.exists(realstring[1]):
                if os.path.isfile(realstring[1]):
                    clientsock.send(str(os.path.getsize(realstring[1])) + "\r\n")
                else:
                    clientsock.send("ERR_ISDIR\r\n")
            else:
                clientsock.send("ERR_NOEXIST\r\n")

    elif realstring[0] == "ENDING":
        if os.name == "nt" or os.name == "ce" or os.name == "os2": # I think all the other OSes use unix line endings
            clientsock.send("WIN")
        else:
            clientsock.send("POSIX")
    
    else:
        pass

    clientsock.shutdown(sock.SHUT_RDWR)
    clientsock.close()

s.shutdown(sock.SHUT_RDWR)
s.close()

#!/usr/bin/env python
"""Copyright (c) 2012, <NAME>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the WOF nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <NAME> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."""
import os, sys, re
import socket as sock

s = sock.socket(sock.AF_INET, sock.SOCK_STREAM)
s.setsockopt(sock.SOL_SOCKET, sock.SO_REUSEADDR, 1)
s.bind(("192.168.155.105", 1337))

s.listen(1)

globaldead = False

while not globaldead:
    dead = False

    clientsock, addr = s.accept()

    string = clientsock.recv(1024)
    realstring = string.split()

    if len(realstring) < 1: # if we get an empty message, ignore it
        continue

    elif realstring[0] == "LIST":
        ret = ""
        if len(realstring) == 1: # if there's only LIST and no directory, assume .
            wanteddir = "."
        else:
            wanteddir = realstring[1]

        if os.path.exists(wanteddir):
            if os.path.isdir(wanteddir):
                for contents in os.listdir(wanteddir):
                    if os.path.isdir(os.path.join(wanteddir, contents)):
                        ret += ("D\t" + contents + "\r\n")
                    else:
                        ret += ("F\t" + contents + "\r\n")

                        clientsock.sendall(ret)
            else:
                clientsock.send("ERR_ISFILE\r\n") # we can't exactly list a file, can we?
        else:
            clientsock.send("ERR_NOEXIST\r\n")

    elif realstring[0] == "GET":
        if len(realstring) == 1:
            clientsock.send("ERR_NOARG\r\n")
        elif os.path.isdir(realstring[1]):
            clientsock.send("ERR_ISDIR\r\n")
        else:
            f = open(realstring[1], "r")
            data = f.read(4096)
            while data:
                clientsock.send(data)
                data = f.read(4096)

    elif realstring[0] == "SIZE":
        if len(realstring) == 1:
            clientsock.send("ERR_NOARG\r\n")
        else:
            if os.path.exists(realstring[1]):
                if os.path.isfile(realstring[1]):
                    clientsock.send(str(os.path.getsize(realstring[1])) + "\r\n")
                else:
                    clientsock.send("ERR_ISDIR\r\n")
            else:
                clientsock.send("ERR_NOEXIST\r\n")

    elif realstring[0] == "ENDING":
        if os.name == "nt" or os.name == "ce" or os.name == "os2": # I think all the other OSes use unix line endings
            clientsock.send("WIN")
        else:
            clientsock.send("POSIX")
    
    else:
        pass

    clientsock.shutdown(sock.SHUT_RDWR)
    clientsock.close()

s.shutdown(sock.SHUT_RDWR)
s.close()
#!/usr/bin/env python Copyright (c) 2012, <NAME> All rights reserved.  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:     * Redistributions of source code must retain the above copyright       notice, this list of conditions and the following disclaimer.     * Redistributions in binary form must reproduce the above copyright       notice, this list of conditions and the following disclaimer in the       documentation and/or other materials provided with the distribution.     * Neither the name of the WOF nor the       names of its contributors may be used to endorse or promote products       derived from this software without specific prior written permission.  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <NAME> BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # if we get an empty message, ignore it # if there's only LIST and no directory, assume . # we can't exactly list a file, can we? # I think all the other OSes use unix line endings