-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileServer.py
More file actions
63 lines (50 loc) · 1.84 KB
/
FileServer.py
File metadata and controls
63 lines (50 loc) · 1.84 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from socket import *
import socket
import threading
import logging
import time
import sys
from FileProtocol import FileProtocol
fp = FileProtocol()
class ProcessTheClient(threading.Thread):
def __init__(self, connection, address):
self.connection = connection
self.address = address
self.data_received = ""
threading.Thread.__init__(self)
def run(self):
while True:
data = self.connection.recv(4096)
if data:
self.data_received += data.decode()
if "\r\n\r\n" in self.data_received:
d = self.data_received.strip()
hasil = fp.proses_string(d)
hasil = hasil + "\r\n\r\n"
self.connection.sendall(hasil.encode())
self.data_received = "" #-- Resetting message buffer --#
else:
break
self.connection.close()
class Server(threading.Thread):
def __init__(self,ipaddress='0.0.0.0', port=6666):
self.ipinfo=(ipaddress,port)
self.the_clients = []
self.my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.my_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
threading.Thread.__init__(self)
def run(self):
logging.warning(f"server berjalan di ip address {self.ipinfo}")
self.my_socket.bind(self.ipinfo)
self.my_socket.listen(1)
while True:
self.connection, self.client_address = self.my_socket.accept()
logging.warning(f"connection from {self.client_address}")
clt = ProcessTheClient(self.connection, self.client_address)
clt.start()
self.the_clients.append(clt)
def main():
svr = Server(ipaddress='0.0.0.0',port=6666)
svr.start()
if __name__ == "__main__":
main()