Skip to content

Instantly share code, notes, and snippets.

@twilight1794
Created September 30, 2017 03:50
Show Gist options
  • Save twilight1794/595611119e30f9e2d7a737ce56908a78 to your computer and use it in GitHub Desktop.
Save twilight1794/595611119e30f9e2d7a737ce56908a78 to your computer and use it in GitHub Desktop.
QOTD server
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Date of version: 5/may/2017 (G. C.)
# A generic class
# Copyright © 2016 Giovanni Alfredo Garciliano Díaz
# This file is part of rdhttpd
# rdhttpd is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# rdhttpd is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with rdhttpd. If not, see <http://www.gnu.org/licenses/>.
import socket
import threading
from printf import printf
import random
class server(object):
""" Base class for implementing any server"""
def __init__(self, host, port, timeout, listen, log, quotes):
self.timeout = timeout
self.listenn = listen
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
printf("qotd: Created a TCP socket", 5)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind((host, port))
printf("qotd: Socket hearing on port" + str(port), 4)
self.linearray = []
try:
reader = open(quotes, 'r')
except PermissionError:
printf("qotd: Unauthorized for access to the file" + quotes, 1)
exit(1)
except FileNotFoundError:
printf("qotd: File" + quotes + "doesn't exists", 1)
exit(1)
except:
printf("qotd: An error has occurred with the file" + quotes , 2)
exit(1)
else:
printf("qotd: File" + quotes + "read", 4)
for line in reader.readlines():
self.linearray.append(line)
def listen(self):
self.socket.listen(self.listenn)
while True:
client, address = self.socket.accept()
client.settimeout(self.timeout)
threading.Thread(target = self.listenToClient,args = (client,address)).start()
def listenToClient(self, client, address):
sendmsg = self.server()
objectbyte = sendmsg.encode()
client.send(objectbyte)
client.close()
def server(self):
quote = random.choice(self.linearray)
quote = quote.split("\t")
response = quote[0] + "\n" + quote[1] + ", " + quote[2] + " " + quote[3]
return response
try:
if len(sys.argv) != 6:
raise exceptions.ArgumentError("You must pass: HOST, PORT, TIMEOUT, LISTEN, LOG, QUOTES")
else:
parameters = []
for i in sys.argv:
parameters.append(i)
parameters[0] = str(parameters[0]) # HOST
parameters[1] = int(parameters[1]) # PORT
parameters[2] = int(parameters[2]) # TIMEOUT
parameters[3] = int(parameters[3]) # LISTEN
parameters[4] = str(parameters[4]) # LOG
parameters[5] = str(parameters[5]) # QUOTES
myserv.listen()
except ValueError:
printf("qotd: The type of the parameters are incorrect. Exiting...", 1)
except ArgumentError:
pass
except PermissionError:
printf("qotd: You must be superuser for use the port 17. Exiting...", 1)
except KeyboardInterrupt:
printf("qotd: ^C pressed. Exiting...", 4)
except:
printf("qotd: An error has occurred. Exiting...", 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment