Я пытаюсь декодировать пользовательский ввод с веб -сайта в файл открытого текста на RPI Pico W, работающий с микропитоном. Я сталкиваюсь с проблемами, когда определенные символы декодировали, в частности, пространства < /p>
Код запускает HTML, чтобы кто -то мог ввести имя пользователя WiFi, а затем возвращает его в PICO, чтобы подключиться к Интернету < /p>
# Manual URL-decoding function
def url_decode(URL):
"""Manually decode a URL-encoded string."""
counter = 0
while '%' in url and counter < 100: # A check to break out of the loop after 100 iterations
index = url.index('%')
# Extract hex code after '%'
hex_code = url[index + 1:index + 3]
# Handle special cases (e.g., "%20" is a space, "%2B" is a plus)
if hex_code == '20': # "%20" represents a space
char = ' '
elif hex_code == '2B': # "%2B" represents the plus sign (+)
char = '+'
else:
try:
# Decode other URL-encoded characters
char = chr(int(hex_code, 16))
except ValueError:
char = '%' # In case of invalid encoding, retain '%'
# Replace the encoded part with the decoded character
url = url[:index] + char + url[index + 3:]
counter += 1
return url
< /code>
В настоящее время, если вы введете такие данные, как
hello+ -, 123.
Вы получите
HeloLo++ -× ,+123.образное. пароль, который введен вторым. Я всегда могу просто удалить последний персонаж, но хотел бы, чтобы он просто не был там, чтобы начать с < /p>
, если это помогает, это другая функция, которая обрабатывает анализ и сохранение данных < /p>
def parse_post_data(request):
"""Parse POST data and return ssid and password."""
try:
# Extract 'ssid' and 'password' from POST request (no URL encoding here)
match_ssid = request.split('ssid=')[1].split('&')[0] # Get the SSID
match_password = request.split('password=')[1].split('&')[0] # Get the password
# Manually decode the URL-encoded data
ssid = url_decode(match_ssid).strip() # Decode URL and strip spaces
password = url_decode(match_password).strip() # Decode URL and strip spaces
print(f"Decoded SSID: {ssid}")
print(f"Decoded Password: {password}")
return ssid, password
except Exception as e:
print(f"Error parsing POST data: {e}")
return None, None
def save_wifi_config(ssid, password):
"""Save the Wi-Fi configuration to a file."""
try:
# Save to the file directly as plain text (no encoding)
with open('wifi_config.txt', 'w') as f:
f.write(f"{ssid}\n{password}\n")
print(f"Wi-Fi credentials saved to wifi_config.txt")
except Exception as e:
print(f"Error saving Wi-Fi credentials: {e}")
Подробнее здесь: https://stackoverflow.com/questions/796 ... icropython
Декодирование URL на микропитоне ⇐ Html
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение