|
|
|
|
@ -152,7 +152,115 @@ except KeyboardInterrupt: |
|
|
|
|
print('Hello user you have pressed ctrl-c button.') |
|
|
|
|
~~~ |
|
|
|
|
|
|
|
|
|
## create a simple chat with python |
|
|
|
|
|
|
|
|
|
[https://python.plainenglish.io/create-a-basic-lan-chat-room-with-python-f334776bf70c](https://python.plainenglish.io/create-a-basic-lan-chat-room-with-python-f334776bf70c) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fixed code |
|
|
|
|
|
|
|
|
|
===client=== |
|
|
|
|
|
|
|
|
|
~~~python |
|
|
|
|
import socket |
|
|
|
|
import threading |
|
|
|
|
|
|
|
|
|
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
|
|
|
|
|
|
|
|
PORT = 8000 |
|
|
|
|
ADDRESS = "localhost" # Same as "127.0.1.1" |
|
|
|
|
|
|
|
|
|
my_socket.connect((ADDRESS, PORT)) |
|
|
|
|
|
|
|
|
|
nickname = input("Choose your nickname : ").strip() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while not nickname: |
|
|
|
|
nickname = input("Your nickname should not be empty : ").strip() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def thread_sending(): |
|
|
|
|
while True: |
|
|
|
|
message_to_send = input("your message :") |
|
|
|
|
if message_to_send: |
|
|
|
|
message_with_nickname = nickname + " : " + message_to_send |
|
|
|
|
my_socket.send(message_with_nickname.encode()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def thread_receiving(): |
|
|
|
|
while True: |
|
|
|
|
message = my_socket.recv(1024).decode() |
|
|
|
|
print(message) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
thread_send = threading.Thread(target=thread_sending) |
|
|
|
|
thread_receive = threading.Thread(target=thread_receiving) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
thread_send.start() |
|
|
|
|
thread_receive.start() |
|
|
|
|
~~~ |
|
|
|
|
|
|
|
|
|
===server=== |
|
|
|
|
|
|
|
|
|
~~~python |
|
|
|
|
import socket |
|
|
|
|
import threading |
|
|
|
|
|
|
|
|
|
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
|
|
|
|
|
|
|
|
PORT = 8000 |
|
|
|
|
ADDRESS = "0.0.0.0" |
|
|
|
|
|
|
|
|
|
broadcast_list = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my_socket.bind((ADDRESS, PORT)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def thread_accept(): |
|
|
|
|
while True: |
|
|
|
|
my_socket.listen() |
|
|
|
|
client, client_address = my_socket.accept() |
|
|
|
|
broadcast_list.append(client) |
|
|
|
|
start_listenning_thread(client) |
|
|
|
|
|
|
|
|
|
def start_listenning_thread(client): |
|
|
|
|
client_thread = threading.Thread( |
|
|
|
|
target = listen_thread, |
|
|
|
|
args = (client,) #the list of argument for the function |
|
|
|
|
) |
|
|
|
|
client_thread.start() |
|
|
|
|
|
|
|
|
|
def listen_thread(client): |
|
|
|
|
while True: |
|
|
|
|
message = client.recv(1024).decode() |
|
|
|
|
if message: |
|
|
|
|
print(f"Received message : {message}") |
|
|
|
|
broadcast(message) |
|
|
|
|
else: |
|
|
|
|
print(f"client has been disconnected : {client}") |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def broadcast(message): |
|
|
|
|
for client in broadcast_list: |
|
|
|
|
try: |
|
|
|
|
client.send(message.encode()) |
|
|
|
|
except: |
|
|
|
|
broadcast_list.remove(client) |
|
|
|
|
print(f"Client removed : {client}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
thread_accept() |
|
|
|
|
|
|
|
|
|
~~~ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-------- |
|
|
|
|
|
|
|
|
|
## create folder in ```apache``` server |
|
|
|
|
|
|
|
|
|
|