-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytest_simplesecserver.py
67 lines (57 loc) · 1.83 KB
/
pytest_simplesecserver.py
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
# first of all import the socket library
import socket
# next create a socket object
s = socket.socket()
print ("Socket successfully created")
port = 12344
# Next bind to the port
# we have not typed any ip in the ip field
# instead we have inputted an empty string
# this makes the server listen to requests
# coming from other computers on the network
s.bind(('', port))
print ("socket binded to %s" %(port))
# put the socket into listening mode
s.listen(5)
print ("socket is listening")
# a forever loop until we interrupt it or
# an error occurs
while True:
# Establish connection with client.
c, addr = s.accept()
print ('Got connection from', addr)
# send a thank you message to the client.
c.send('Thank you for connecting')
# Close the connection with the client
c.close()
def encrypt(string, shift):
shift=2
cipher = ''
for char in string:
if char == ' ':
cipher = cipher + char
elif char.isupper():
cipher = cipher + chr((ord(char) + shift - 65) % 26 + 65)
else:
cipher = cipher + chr((ord(char) + shift - 97) % 26 + 97)
return cipher
def decrypt(string, shift):
shift=2
cipher = ''
for char in string:
if char == ' ':
cipher = cipher + char
elif char.isupper():
cipher = cipher + chr((ord(char) - shift - 65) % 26 + 65)
else:
cipher = cipher + chr((ord(char) - shift - 97) % 26 + 97)
return cipher
text = input("enter plaintext: ")
#s = int(input("enter shift number: "))
print("original string: ", text)
print("after encryption: ", encrypt(text, 2))
text = input("enter ciphertext: ")
print("your input: ", text)
print("after decryption: ", decrypt(text, 2))
# Close the connection with the client
#c.close()