Created
November 18, 2020 21:37
-
-
Save rikatz/80bafc7cad06e7c8508e65d78a78d35e to your computer and use it in GitHub Desktop.
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 programming library | |
import socket | |
# import thread module | |
from _thread import * | |
import threading | |
print_lock = threading.Lock() | |
class frame(): | |
def __init__(self, frame): | |
offset = 0 # In what character are we? | |
while offset < len(frame): | |
frame_length = frame[offset:offset+4] | |
offset +=3 | |
frame_type = frame[offset] | |
#offset += frame_length | |
print(frame_length.hex()) | |
print(int(frame_type)) | |
# thread function | |
def threaded(c): | |
while True: | |
# data received from client | |
data = c.recv(1024) | |
if not data: | |
# lock released on exit | |
print_lock.release() | |
break | |
# reverse the given string from client | |
data = data[::-1] | |
# send back reversed string to client | |
frame(data) | |
# connection closed | |
c.close() | |
def Main(): | |
host = "" | |
# reverse a port on your computer | |
# in our case it is 12345 but it | |
# can be anything | |
port = 12345 | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.bind((host, port)) | |
print("socket binded to port", port) | |
# put the socket into listening mode | |
s.listen(5) | |
print("socket is listening") | |
# a forever loop until client wants to exit | |
while True: | |
# establish connection with client | |
c, addr = s.accept() | |
# lock acquired by client | |
print_lock.acquire() | |
print('Connected to :', addr[0], ':', addr[1]) | |
# Start a new thread and return its identifier | |
start_new_thread(threaded, (c,)) | |
s.close() | |
if __name__ == '__main__': | |
Main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment