Last active
June 20, 2018 08:12
-
-
Save maulvi/326d891a21486b07601b4d8d7253e923 to your computer and use it in GitHub Desktop.
Socket chat room Caesar cipher
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 select | |
import sys | |
import string | |
key = 'abcdefghijklmnopqrstuvwxyz' | |
def encrypt(n, plaintext): | |
"""Encrypt the string and return the ciphertext""" | |
result = '' | |
for l in plaintext.lower(): | |
try: | |
i = (key.index(l) + n) % 26 | |
result += key[i] | |
except ValueError: | |
result += l | |
return result.lower() | |
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
if len(sys.argv) != 3: | |
print "script, IP address, port number" | |
exit() | |
IP_address = str(sys.argv[1]) | |
Port = int(sys.argv[2]) | |
server.connect((IP_address, Port)) | |
while True: | |
sockets_list = [sys.stdin, server] | |
read_sockets,write_socket, error_socket = select.select(sockets_list, [], []) | |
for socks in read_sockets: | |
if socks == server: | |
message = socks.recv(2048) | |
print message | |
else: | |
offset = 4 | |
text = message | |
encrypted = encrypt(offset, text) | |
message = sys.stdin.readline() | |
server.send(encrypted) | |
sys.stdout.write(encrypted) | |
sys.stdout.flush() | |
server.close() |
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 select | |
from thread import * | |
import sys | |
import string | |
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
# check argument yang dimasukkan lengkap dan benar | |
if len(sys.argv) != 3: | |
print "Correct usage: script, IP address, port number" | |
exit() | |
# perintah bind alamat ip address | |
IP_address = str(sys.argv[1]) | |
# perintah bind port number | |
Port = int(sys.argv[2]) | |
server.bind((IP_address, Port)) | |
server.listen(100) | |
list_of_clients = [] | |
key = 'abcdefghijklmnopqrstuvwxyz' | |
def decrypt(n, ciphertext): | |
"""Decrypt string dan return dengan hasil plaintext""" | |
result = '' | |
for l in ciphertext: | |
try: | |
i = (key.index(l) - n) % 26 | |
result += key[i] | |
except ValueError: | |
result += l | |
return result | |
def clientthread(conn, addr): | |
# sends a message to the client whose user object is conn | |
conn.send("Welcome to chatroom!") | |
while True: | |
try: | |
message = conn.recv(2048) | |
if message: | |
offset = 4 | |
text = message | |
decrypted = decrypt(offset, text) | |
print "<" + addr[0] + "> " + decrypted | |
# Calls broadcast function to send message to all | |
message_to_send = "<" + addr[0] + "> " + decrypted | |
broadcast(message_to_send, conn) | |
else: | |
"""message may have no content if the connection | |
is broken, in this case we remove the connection""" | |
remove(conn) | |
except: | |
continue | |
"""broadcast pesan ke semua client""" | |
def broadcast(message, connection): | |
for clients in list_of_clients: | |
if clients!=connection: | |
try: | |
clients.send(message) | |
except: | |
clients.close() | |
# if the link is broken, we remove the client | |
remove(clients) | |
"""The following function simply removes the object | |
from the list that was created at the beginning of | |
the program""" | |
def remove(connection): | |
if connection in list_of_clients: | |
list_of_clients.remove(connection) | |
while True: | |
conn, addr = server.accept() | |
"""List client yang terhubung pada room agar tetap terhubung""" | |
list_of_clients.append(conn) | |
# prints the address of the user that just connected | |
print addr[0] + " connected" | |
# creates and individual thread for every user | |
# that connects | |
start_new_thread(clientthread,(conn,addr)) | |
conn.close() | |
server.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment