Skip to content

Commit 3813886

Browse files
authored
Merge pull request #12 from AcidOP/master
Added a simple TCP server with the socket library in python3
2 parents dbe44db + 29d6573 commit 3813886

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,6 @@ DigitalClock.py
4848

4949
Generate a random password
5050
GeneratePassword.py
51+
52+
## Script 10 - Simple TCP Chat Server
53+
server.py - Creates a local server on your LAN for receiving and sending messages!

server.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import socket
2+
3+
IP = '0.0.0.0'
4+
PORT = 3000
5+
6+
def main():
7+
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8+
server.bind((IP, PORT))
9+
10+
server.listen(5)
11+
print(f'Server is listening on {IP}:{PORT}')
12+
13+
while True:
14+
client, address = server.accept()
15+
print(f'Received a connection from {address[0]}:{address[1]}')
16+
17+
data = client.recv(4096)
18+
decoded_data = data.decode()
19+
print(decoded_data)
20+
21+
send = input('Send message> ')
22+
client.send(send.encode())
23+
24+
25+
if __name__ == '__main__':
26+
main()

0 commit comments

Comments
 (0)