Created
October 14, 2022 16:32
-
-
Save Misterguruman/04d5047897cbb1796b7f4cac98ee2f22 to your computer and use it in GitHub Desktop.
A simple TCP client and server turned chat application back when I was learning Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import argparse | |
def Main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('ip', help="Specify the ip address the program will run on here", type=str) | |
parser.add_argument('port', help="Specift the port the program will run on here", type=int) | |
group = parser.add_mutually_exclusive_group() | |
group.add_argument('-s', '--server', help="To run as a server", action="store_true") | |
group.add_argument('-c', '--client', help="To run as a client", action="store_true") | |
args = parser.parse_args() | |
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) | |
if args.server: | |
s.bind((args.ip, args.port)) | |
s.listen(1) | |
c, addr = s.accept() | |
print "You have recieved a connection from "+str(addr) | |
if True: | |
c.send("Thanks for connecting to the server") | |
while True: | |
data = c.recv(1024) | |
if not data: | |
break | |
print "Recieved : "+str(data) | |
c.close() | |
if args.client: | |
s.connect((args.ip, args.port)) | |
print "Type what you would like to send, type q to exit" | |
message = raw_input("Send Message: ") | |
data = s.recv(1024) | |
print data | |
while message != 'q': | |
s.send(message) | |
message = raw_input("-> ") | |
s.close() | |
if __name__ == '__main__': | |
Main() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Simple TCP 2-way Chat | |
Author: Joseph Paul Langford | |
Contact: Joseph.P.Langford@gmail.com | |
""" | |
import socket | |
import threading | |
from time import sleep | |
import argparse | |
import os | |
def getip(): | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect(('www.google.com', 80)) | |
ip = s.getsockname()[0] | |
s.close() | |
return ip | |
class ChatServ(threading.Thread): | |
def __init__(self, port): | |
threading.Thread.__init__(self) | |
self.port = port | |
self.ip = getip() | |
def run(self): | |
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self.s.bind((self.ip, self.port)) | |
print "Server running on "+str(self.ip)+" on port "+str(self.port) | |
self.s.listen(1) | |
self.c, self.addr = self.s.accept() | |
print "Connection made from "+str(self.addr) | |
while True: | |
self.data = self.c.recv(1024) | |
if self.data == 'q': | |
break | |
else: | |
print self.data | |
self.s.close() | |
class ChatCli(threading.Thread): | |
def __init__(self, ip, port): | |
threading.Thread.__init__(self) | |
self.ip = ip | |
self.port = port | |
def run(self): | |
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self.s.connect((self.ip, self.port)) | |
print "Connection Made! Send q to quit." | |
sleep(.1) | |
self.message = raw_input("-> ") | |
while self.message != 'q': | |
self.s.send(self.message) | |
sleep(.1) | |
self.message = raw_input("-> ") | |
self.s.send(self.message) | |
self.s.close() | |
def Main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument("Client_IP", help="Enter the ip address of the client computer", type=str) | |
parser.add_argument("-p", "--port", help="Uses alternative port", action="store_true") | |
args = parser.parse_args() | |
if args.port: | |
port = 45573 | |
else: | |
port = 5000 | |
cs = ChatServ(port) | |
cs.start() | |
sleep(2) | |
if os.name == 'nt': | |
os.system('cls') | |
elif os.name == 'posix': | |
os.system('clear') | |
cc = ChatCli(args.Client_IP, port) | |
cc.start() | |
if __name__ == '__main__': | |
Main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment