-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtcpServer.py
More file actions
36 lines (30 loc) · 1.04 KB
/
tcpServer.py
File metadata and controls
36 lines (30 loc) · 1.04 KB
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
33
34
35
import os
import re
from socket import *
def get_ip(ifaces=['wlan1', 'eth0', 'wlan0']):
if isinstance(ifaces, str):
ifaces = [ifaces]
for iface in list(ifaces):
search_str = f'ifconfig {iface}'
result = os.popen(search_str).read()
com = re.compile(r'(?<=inet )(.*)(?= netmask)', re.M)
ipv4 = re.search(com, result)
if ipv4:
ipv4 = ipv4.groups()[0]
return ipv4
return ''
server_port = 12000
server_ip = get_ip('lo0')
print(server_ip,server_port)
if __name__ == "__main__":
server_socket = socket(AF_INET,SOCK_STREAM)
server_socket.bind((server_ip, server_port))
server_socket.listen(1)
print(f"The server is ready on ({server_ip}, {server_port}).")
while True:
connection_socket, addr = server_socket.accept()
message = connection_socket.recv(1024).decode()
print(f'MESSAGE RECEIVED:'+message)
modified_message = message.upper()
connection_socket.send(modified_message.encode())
connection_socket.close()