-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.py
More file actions
79 lines (72 loc) · 2.57 KB
/
server.py
File metadata and controls
79 lines (72 loc) · 2.57 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python3
# server script to initialize the server with
# internal or external network
# Note : please share the IP Address and Port with the
# client(s) to start chatting.
import socket, getpass
import sys, os
from os import system, path
from time import sleep
from platform import system as systemos, architecture
from simplecrypt import encrypt,decrypt
# using serveo.net to forward port for external network communication
def runServeo():
print("Connecting to External network....")
system('ssh -R 9568:0.0.0.0:9568 serveo.net > /dev/null &')
sleep(5)
ip = socket.gethostbyname('serveo.net')
print("IP: {} \t PORT: 9568".format(ip))
print("Share the above IP and PORT number with client.")
# checking if user needs to connect through Internal Network or External Network
def InternalExternal():
mode = input("Chat on Internal or External Network ? (I/E): ")
if mode.lower() == 'e':
return True
elif mode.lower() == 'i':
return False
else:
print("Choose correct option !")
InternalExternal()
# actual code to chat over the network
# the message sent and received are encoded as 'UTF-8'
# change the 42 line for utilizing number of connections to accept
def chat(host,port):
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # creating of socket
server.bind((host, port)) # binding the host and port
server.listen(5)
print("Waiting for connection from the client...")
client, address = server.accept() #accepting connection form client
system('clear')
print('Got connection from : ', address)
name = input("Your name : ") # name to identify yourself
# set password to encrypt and decrypt the chat
passwd = getpass.getpass("Enter password for encrypted chat : ") # password to encrypt chat msgs
while True:
print(decrypt(passwd,client.recv(1024)).decode('utf-8')) # decrypt the received encrypted msg
msg = input("Me : ")
encMsg = encrypt(passwd,(name+" : "+msg)) # encrypt the msg
if msg.lower() == "bye":
client.send(encMsg) # send encrypted msg
client.close()
server.close()
system("pkill -f 'ssh -R 9568:0.0.0.0:9568 serveo.net'")
exit(0)
else:
client.send(encMsg) # send encrypted msg
if __name__ == '__main__':
host = ''
if InternalExternal():
runServeo()
host = "0.0.0.0"
port = 9568
else:
print("Your local IP/host IP is set to :")
str(list(str(os.system("hostname -I"))).remove('0'))
host = input("Enter the host : ")
port = int(input("Enter the port : "))
try:
chat(host,port)
except KeyboardInterrupt:
print("\nKeyboard Interrupted ! \nBye bye..")
system("pkill -f 'ssh -R 9568:0.0.0.0:9568 serveo.net'")
exit()