-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.py
More file actions
32 lines (22 loc) · 746 Bytes
/
server.py
File metadata and controls
32 lines (22 loc) · 746 Bytes
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
import socket
# Create a socket object
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Get local machine name
host = socket.gethostname()
# Define the port on which you want to connect
port = 12345
# Bind the socket to the address and port
server_socket.bind((host, port))
# Now wait for a client to connect
server_socket.listen(1)
print(f'Waiting for connections on {host}:{port}...')
# Accept the connection
client_socket, addr = server_socket.accept()
print('Got connection from', addr)
while True:
data = client_socket.recv(1024).decode('utf-8')
print('Received:', data)
message = input('Enter your message: ')
client_socket.send(message.encode('utf-8'))
# Close the socket
client_socket.close()