Skip to content

Commit 2410c51

Browse files
committed
simplify the TCP server-client scripts
Adapt to use Python3 syntax which is way better and recommended by the Python community. The scripts were based from https://shakeelosmani.wordpress.com/2015/04/13/python-3-socket-programming-example/ Resolves: Related: Signed-off-by: Daniel Andrei Minca <mandrei17@gmail.com>
1 parent 6c21168 commit 2410c51

File tree

2 files changed

+39
-40
lines changed

2 files changed

+39
-40
lines changed

tcp_client.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1-
#!/usr/bin/env python3
1+
#!/usr/bin/env python
22
# SIMPLE TCP CLIENT
33

44
import socket
55

6-
host = 'www.google.com'
7-
port = 80
6+
def Client():
7+
host = '127.0.0.1'
8+
port = 5000
89

9-
# create socket obj
10-
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
10+
mySocket = socket.socket()
11+
mySocket.connect((host, port))
1112

12-
# connect
13-
client.connect((host, port))
13+
message = input('-> ')
1414

15-
# send data
16-
client.send("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")
15+
while message != 'q':
16+
mySocket.send(message.encode())
17+
data = mySocket.recv(1024).decode()
1718

18-
# receive data
19-
response = client.recv(4096)
19+
print('Received from server: ' + data)
2020

21-
print(response)
21+
message = input('-> ')
22+
23+
mySocket.close()
24+
25+
if __name__ == '__main__':
26+
Client()

tcp_server.py

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,34 @@
1-
#!/usr/bin/env python3
1+
#!/usr/bin/env python
22
# SIMPLE TCP SERVER IMPLEMENTATION IN PYTHON
3+
# inspired from:
4+
# https://shakeelosmani.wordpress.com/2015/04/13/python-3-socket-programming-example/
35

46
import socket
5-
import threading
67

7-
bind_ip = '0.0.0.0'
8-
bind_port = 9999
8+
def Server():
9+
host = '127.0.0.1'
10+
port = 5000
911

10-
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
12+
mySocket = socket.socket()
13+
mySocket.bind((host, port))
1114

12-
server.bind((bind_ip, bind_port))
15+
mySocket.listen(1)
16+
conn, addr = mySocket.accept()
1317

14-
server.listen(5)
18+
print("Connection from: " + str(addr))
1519

16-
print("[*] Listening on %s:%d" % (bind_ip, bind_port))
20+
while True:
21+
data = conn.recv(1024).decode()
22+
if not data:
23+
break
1724

18-
# client-handling thread
19-
def handle_client(client_socket):
25+
print('from connected user: ' + str(data))
2026

21-
# print what client sends
22-
request = client_socket.recv(1024)
27+
data = str(data).upper()
28+
print('sending: ' + str(data))
29+
conn.send(data.encode())
2330

24-
print("[*] Received: %s" % request)
25-
26-
# send back packet
27-
client_socket.send("ACK!")
28-
29-
client_socket.close()
30-
31-
while True:
32-
33-
client,addr = server.accept()
34-
35-
print("[*] Accepted connection from: %s:%d" % (addr[0], addr[1]))
36-
37-
# spin client thread to handle incomming data
38-
client_handler = threading.Thread(target = handle_client, args = (client,))
39-
client_handler.start()
31+
conn.close()
4032

33+
if __name__ == '__main__':
34+
Server()

0 commit comments

Comments
 (0)