3
\$\begingroup\$

This is a little chat program that I have been working on and I want general feedback such as feature suggestions, efficiency comments, etc...

The server will setup a DGRAM socket and constantly listen for any data. The client will start and send a "connect" message and then the server will send said message to the listeners.

The client can also send a shutdown request by sending this: liugliuaeghuire9uhg35oq9hg8ovubtg78oHAEP0GOwv87ubiyogbugWgo8yuo8BGV87BwoiegboIB This will cause the client to close as well.

The clients can be shut down by sending: q The listeners are shut down by closing the program.

The server will also create a log file every time it is started.

Server:

#Sorry for no comments...

import socket

from time import gmtime, strftime

import time

def Main():

    host = "localhost"

    port = 5000

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    s.bind((host,port))

    listeners = []

    clients = []

    log = open("Log.txt", "w")

    log.write("--------------Server started at {}--------------".format(strftime("%Y-%m-%d %H:%M:%S", gmtime()))+ "\n")

    print("Server Started")

    while True:

        try:

            data, addr = s.recvfrom(1024)

            data = data.decode("utf-8")

            if "luhgauhlgahughahilgauifgaliurGH;AUOGHO;BVA" in data:

                if addr not in listeners:

                    listeners.append(addr)

                    print("({})New listner connected: {}".format(strftime("%Y-%m-%d %H:%M:%S", gmtime()),addr)+ "\n")

                    log.write("({})New listner connected: {}".format(strftime("%Y-%m-%d %H:%M:%S", gmtime()),addr)+ "\n")

            elif "ilghaiughlisahgraliugWAOH;VLRIUBEIALIUEBAI" in data:

                if addr not in clients:

                    clients.append(addr)

                    print("({})New client connected: {}".format(strftime("%Y-%m-%d %H:%M:%S", gmtime()),addr)+ "\n")

                    log.write("({})New client connected: {}".format(strftime("%Y-%m-%d %H:%M:%S", gmtime()),addr)+"\n")

            elif "liugliuaeghuire9uhg35oq9hg8ovubtg78oHAEP0GOwv87ubiyogbugWgo8yuo8BGV87BwoiegboIB" in data:

                print("Shutdown request recieved from {} at {}".format(addr,strftime("%Y-%m-%d %H:%M:%S", gmtime())+ "\n"))

                log.write("Shutdown request recieved from {} at {}".format(addr,strftime("%Y-%m-%d %H:%M:%S", gmtime())+ "\n"))

                for i in range(len(listeners)):

                    s.sendto("Server: Shutting down in five seconds.".encode("utf-8"), listeners[i])
                    s.sendto("dfiasgheoivuhaeoiufgahoiguvhaeoifuegoiufgaf7483q4ogv".encode("utf-8"), listeners[i])

                time.sleep(5)

                break

            elif "adfbheiuarbfjklaeglhuaerliguhadfliuvbaile" in data:

                if addr in clients:

                    clients.remove(addr)

                    print("({}) has disconnected: {}".format(addr,strftime("%Y-%m-%d %H:%M:%S", gmtime()))+ "\n")

                    for i in range(len(listeners)):

                        s.sendto("({}) has disconnected: {}".format(addr,strftime("%Y-%m-%d %H:%M:%S", gmtime()))+ "\n".encode("utf-8"),listeners[i])             

            else:

                print("{} ({}) {}".format(strftime("%Y-%m-%d %H:%M:%S", gmtime()), addr, data))

                log.write("{} ({}) {}".format(addr, strftime("%Y-%m-%d %H:%M:%S", gmtime()), data)+ "\n")

                for i in range(len(listeners)):

                    s.sendto(data.encode("utf-8"),listeners[i])
        except:

            pass

    s.close()

    log.close()

if __name__ == "__main__":
    Main()

Client:

#Sorry for no comments
import socket, time

def Main():
    host = input("Enter server ip: ")
    port = 0

    server = ("127.0.0.1", 5000)

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    s.bind((host,port))

    s.sendto("ilghaiughlisahgraliugWAOH;VLRIUBEIALIUEBAI".encode("utf-8"), server)

    alias = input("Enter your name: ")

    message = input("-> ")

    while message != "q":

        s.sendto("{}-> {}".format(alias,message).encode("utf-8"), server)

        message = input("-> ")

        if "liugliuaeghuire9uhg35oq9hg8ovubtg78oHAEP0GOwv87ubiyogbugWgo8yuo8BGV87BwoiegboIB" in message:

            print("Shutdown request sent...")

            print("Shutting down...")

            time.sleep(3)

            break

    if "liugliuaeghuire9uhg35oq9hg8ovubtg78oHAEP0GOwv87ubiyogbugWgo8yuo8BGV87BwoiegboIB" not in message:

        s.sendto("adfbheiuarbfjklaeglhuaerliguhadfliuvbaile".encode("utf-8"), server)

    s.close()

if __name__ == "__main__":
    Main()

Listener:

#Sorry for no comments
import socket, time

def Main():
    host = input("Enter server ip: ")

    port = 0

    server = ("127.0.0.1", 5000)

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    s.bind((host,port))

    s.sendto("luhgauhlgahughahilgauifgaliurGH;AUOGHO;BVA".encode("utf-8"), server)

    while True:

        data, addr = s.recvfrom(1024)

        data = data.decode("utf-8")

        if "dfiasgheoivuhaeoiufgahoiguvhaeoifuegoiufgaf7483q4ogv" in data:

            time.sleep(5)

        print(str(data))

if __name__ == "__main__":
    Main()

Instructions:

Launch the server first and then launch as many clients or listeners wanted.


Any feedback would be really appreciated.

\$\endgroup\$
  • \$\begingroup\$ What's it supposed to do? \$\endgroup\$ – Mast Feb 27 '18 at 17:37
  • 1
    \$\begingroup\$ Welcome to Code Review! This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does \$\endgroup\$ – Dannnno Feb 27 '18 at 18:59
  • \$\begingroup\$ Thank you for your comment. The changes requested have been made and I hope this clarifies the code a bit. \$\endgroup\$ – Ghost Feb 28 '18 at 8:18
  • \$\begingroup\$ Why do you use 'random' cryptic strings instead of something more readable? \$\endgroup\$ – Solomon Ucko Feb 28 '18 at 14:49
  • \$\begingroup\$ To prevent users from say connecting another listener when there isn't actually another listener being connected and the shutdown code is self explanatory (Don't want a bad user from shutting down a genuine server.) \$\endgroup\$ – Ghost Feb 28 '18 at 16:26
0
\$\begingroup\$

Nice, BTW this dude made the whole thing in a single file:

https://github.com/Ayanjit/python-UDP-Chat

I write it here in case you need it

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.