This is a simple chat application using python socket library. The script accept the following input arguments:
Argv 1 = enter 1(server) or 2(client)
Argv 2 = port number
Argv 3 = host
To run the program as server: python popchat.py 1 8080 test(any string)
To run the program as a client: python popchat.py 2 8080 hostname
If run on a different pc, replace the host in server with '0.0.0.0' and firewall may have to be disabled(be careful).
I use the following code to disable/enable my firewall:(Windows 10):
1 2 | import subprocess subprocess.check_call('netsh.exe advfirewall set publicprofile state on') |
Use "on" to enable the firewall and "off" to disable.
The output:
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import sys import os import socket def popper(): print("") s.listen(1) print("") conn,addr = s.accept() while 1: command = input(str("Command >> ")) conn.send(command.encode()) files = conn.recv(5000) files = files.decode() print(files) def poppee(): while 1: files = s.recv(5000) files = files.decode() print(files) command = input(str("Command >> ")) s.send(command.encode()) if __name__ == "__main__": s = socket.socket() port=sys.argv[2] print(sys.argv[1],sys.argv[2]) if sys.argv[1] == '1': host=socket.gethostname() print(host) s.bind((host,int(port))) popper() else: host = sys.argv[3] print(host) s.connect((host,int(port))) poppee() |
No comments:
Post a Comment