hello thanks for reading this and helping out, i really appreciate it.
this is only my second project after learning the basics of python/programming. i am trying to make a really basic http server for my own use. it is only going to support GET and POST. i am trying to use the standard library as much as possible just because. and i have imported a few modules but those are there just in case pretty much.
this code is a bit messy. there is a thing or two there that doesnt need to be there, just there because i am trying to figure things out.
my problem here is reading the recieved data. i have not got to the part where i send the client some data yet, because i cannot get this program to tell if the path is in the recieved data or not, i was trying to use a for loop through data3 if client_choice in data3 then send a response. and first i just wanted to be sure that i could read the path: client_choice or not. and i have had absolutely no luck. so thats where im at with this, i deleted the for loop and tried something else and have not changed it back.
any help is really appreciated, thank you.
this is a edit: i just want to say again that this is not complete and the way it is because i am just trying to figure it out bit by bit, thanks for helping
âââ
import os
import socket
import io
import sys
import requests
import csv
import time
import re
Path = list()
list_of_path = list()
client_choice = str(ââ)
download_path = list()
#download_path.join(Path)
garbage = list()
def get_path():
path = input(âenter absolute file path to serve from, and to save POSTâed files in: â)
while not os.path.isdir(path):
print(âpath not accepted, try againâ)
path = input(âenter absolute file path to serve from, and to save POSTâed files in: â)
if os.path.isdir(path):
print(âpath acceptedâ)
for item in os.listdir(path):
list_of_path.append(item)
Path.append(path)
client_choice = path
def make_server():
HOST = â192.168.1.233â
PORT = 8025
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM,) as server1: # this server does not work, it cant read recieved data correctly or at all idk
server1.bind((HOST, PORT))
server1.listen()
print(f"server is working on IP: {HOST} and PORT: {PORT}â)
conn, addr = server1.accept()
print(f"new connection from: {addr}â)
data = conn.recv(4096)
data1 = data.decode()
data3 = data1.split(â â)
data2 = [data1[i:i+50] for i in range(0, len(data1), 50)]
print(data2)
print(data3)
if client_choice in data3: #any(client_choice in x for x in data3) # for loop was here
x = data1.find(client_choice) # changed line.find(client_choice) to data1.find
y = str(ââ)
for letter in data1[x:]:
y += letter
if letter == " â:
download_path.append(y)
conn.send(b"HTTP/1.1 200\r\n\r\nthis is what you see if it read client_choiceâ)
#elif âGETâ in data3:
#conn.send(b"HTTP/1.1 200\r\n\r\nthis is reply to get")
else:
conn.send(b"HTTP/1.1 200\r\n\r\nthis is reply to NO get and NO client choice")
except socket.error:
time.sleep(5)
finally:
make_server()
if name == âmainâ:
get_path()
make_server()
âââ