-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhftp.py
35 lines (24 loc) · 998 Bytes
/
hftp.py
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 socket
import os
from multiprocessing import Process
def handle_client(client_socket):
request_data = client_socket.recv(1024)
response_start_line = "HTTP/1.1 200 ok\r\n"
response_headers = "Server: HFTP\r\n"
response_body = "\n".join(os.listdir("."))
response = response_start_line + response_headers + "\r\n" + response_body
client_socket.send(bytes(response, "utf-8"))
client_socket.close()
if __name__ == "__main__":
hostname = socket.gethostname()
ip = socket.gethostbyname(hostname)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 80))
server_socket.listen()
print("Start working on: " + ip)
while True:
client_socket, client_address = server_socket.accept()
print("[%s, %s] is connected!" % client_address)
handle_client_process = Process(target=handle_client,args=(client_socket,))
handle_client_process.start()
client_socket.close()