Skip to content

Commit 69e2b79

Browse files
committed
Added pychat program
1 parent 7672921 commit 69e2b79

File tree

3 files changed

+258
-0
lines changed

3 files changed

+258
-0
lines changed

pychat/Readme.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Hello everyone to get started with pychat you just need your IP address and any port number ranging from 1-65535
2+
but my recommendations would be take any port number above 1000 as below are already assigned to other services.
3+
This client,server script can work on both remote and local machines.
4+
5+
For Eg:- IP:192.168.0.108(Local IP), 24.18.46.234(Remote IP)
6+
Port:6666,8080,7641
7+
8+
1. For Windows
9+
ipconfig /all
10+
11+
2. For Linux/MacOS
12+
ifconfig
13+
14+
1. First start the server script and enter the IP address and port number
15+
python server.py
16+
[+] Enter server address: 192.168.108
17+
[+] Enter server port: 8888
18+
19+
2. Then start the pychat script
20+
python pychat.py
21+
[+] Enter server address: 192.168.108
22+
[+] Enter server port: 8888

pychat/pychat.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#! /usr/bin/python3
2+
3+
"""
4+
__Description__='This is a simple script which establishes socket connection
5+
between server and client using TCP protocol and python socket
6+
library.'
7+
"""
8+
9+
from socket import *
10+
11+
12+
#Function: To display the banner
13+
def Banner():
14+
print("""
15+
________ ______ _____
16+
___ __ \____ __________ /_______ __ /_
17+
__ /_/ /_ / / / ___/_ __ \ __ `/ __/
18+
_ ____/_ /_/ // /__ _ / / / /_/ // /_
19+
/_/ _\__, / \___/ /_/ /_/\__,_/ \__/
20+
/____/ Version 1.0
21+
""")
22+
23+
24+
#Function: Asking user for server address and port
25+
def User_Input():
26+
27+
28+
#Function: For validating IP address
29+
def Valid_IP():
30+
while True:
31+
try:
32+
host = input("[+] Enter server address: ")
33+
valid_ip = host.split('.')
34+
for items in valid_ip:
35+
if(len(valid_ip) == 4 and int(items)):
36+
return host
37+
else:
38+
print("[+] Enter a valid IP")
39+
continue
40+
except ValueError:
41+
print("[+] Enter a valid IP")
42+
continue
43+
44+
45+
#Function: For validating port number
46+
def Valid_Port():
47+
while True:
48+
try:
49+
port = int(input("[+] Enter server port: "))
50+
if(port > 0 and port < 65535):
51+
pass
52+
else:
53+
print("[+] Port must be 0-65535")
54+
continue
55+
except ValueError:
56+
print("[+] Enter a valid port number")
57+
continue
58+
else:
59+
break
60+
return port
61+
62+
host, port = Valid_IP(), Valid_Port()
63+
64+
return host, port
65+
66+
67+
#Function: To exit or continue the connection
68+
def Client_Exit():
69+
while True:
70+
user_value = input("[+] Want to send more messages? (y/n): ")
71+
if (user_value == 'y' or user_value == 'n'):
72+
return user_value
73+
else:
74+
print("[+] Wrong input")
75+
continue
76+
77+
78+
#Function: To setup and communicate with the server
79+
def Client_Setup():
80+
81+
#Printing the banner
82+
Banner()
83+
84+
#Setting the server address and port
85+
server_address, server_port = User_Input()
86+
87+
#Initializing a new socket connection
88+
my_socket = socket(AF_INET, SOCK_STREAM)
89+
print("[+] Establishing connection")
90+
91+
#Connecting the socket to specified host and port
92+
my_socket.connect((server_address, server_port))
93+
print("[+] Connection established")
94+
95+
#Infinite loop for sending messages to server
96+
while True:
97+
message = input("[+] Message to send: ")
98+
#uncomment the below line if you're using ncat/netcat for better
99+
#terminal readability
100+
#message = message + "\n"
101+
my_socket.send(message.encode('utf-8'))
102+
103+
user_value = Client_Exit()
104+
if (user_value == 'n'):
105+
print("[+] Closing transport")
106+
my_socket.close()
107+
break
108+
else:
109+
continue
110+
111+
112+
try:
113+
Client_Setup()
114+
115+
except ConnectionRefusedError:
116+
print("[+] Connection refused")
117+
118+
except TimeoutError:
119+
print("[+] Connection timeout")
120+
121+
except KeyboardInterrupt:
122+
print("[+] Closing transport")
123+
124+
except OSError:
125+
print("[+] Invalid host")

pychat/server.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#! /usr/bin/python3
2+
3+
"""
4+
__Description__='This is a simple script which establishes socket connection
5+
between server and client using TCP protocol and python socket
6+
library.'
7+
"""
8+
9+
from socket import *
10+
11+
12+
#Function: To display the banner
13+
def Banner():
14+
print("""
15+
__ ___ ___ _ _ ___ ___
16+
/' _/| __| _ \| \ / || __| _ \ \t
17+
`._`.| _|| v /`\ V /'| _|| v /
18+
|___/|___|_|_\ \_/ |___|_|_\ Version 1.0
19+
""")
20+
21+
22+
#Function: Asking user for server setup
23+
def User_Input():
24+
25+
26+
#Function: For validating IP address
27+
def Valid_IP():
28+
while True:
29+
try:
30+
host = input("[+] Enter server address: ")
31+
valid_ip = host.split('.')
32+
for items in valid_ip:
33+
if(len(valid_ip) == 4 and int(items)):
34+
return host
35+
else:
36+
print("[+] Enter a valid IP")
37+
continue
38+
except ValueError:
39+
print("[+] Enter a valid IP")
40+
continue
41+
42+
43+
#Function: For validating port number
44+
def Valid_Port():
45+
while True:
46+
try:
47+
port = int(input("[+] Enter server port: "))
48+
if(port > 0 and port < 65535):
49+
pass
50+
else:
51+
print("[+] Port must be 0-65535")
52+
continue
53+
except ValueError:
54+
print("[+] Enter a valid port number")
55+
continue
56+
else:
57+
break
58+
return port
59+
60+
host, port = Valid_IP(), Valid_Port()
61+
62+
return host, port
63+
64+
65+
#Function: To setup and communicate with the client
66+
def Server_Setup():
67+
68+
#Printing the banner
69+
Banner()
70+
71+
#Setting server address and port
72+
server_address, server_port = User_Input()
73+
74+
#Initializing a new socket connection
75+
my_socket = socket(AF_INET, SOCK_STREAM)
76+
77+
#Setting socket for reuse
78+
my_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
79+
80+
#Binding the new socket to specified host and port
81+
my_socket.bind((server_address, server_port))
82+
83+
#Listening for number of connections simultaneously
84+
my_socket.listen(3)
85+
print("[+] Server started! Waiting for connections...")
86+
87+
#Accepting the connection from client
88+
connection, address = my_socket.accept()
89+
print("[+] Client connected with address: ", address)
90+
91+
#Infinite loop for receiving messages from client
92+
while True:
93+
# 1024 is the buffer size
94+
data = connection.recv(1024)
95+
if not data:
96+
print("[+] Closing transport")
97+
break
98+
else:
99+
print("[+] Client:", data.decode('utf-8'))
100+
101+
connection.close()
102+
103+
104+
try:
105+
Server_Setup()
106+
107+
except KeyboardInterrupt:
108+
print("[+] Closing transport")
109+
110+
except OSError:
111+
print("[+] Invalid host")

0 commit comments

Comments
 (0)