import requests
from bs4 import BeautifulSoup
from urllib3.exceptions import HTTPError
import urllib3
urllib3.disable_warnings()



URL = "https://t.wtware.local/config_edit"

def create_wtc(data):
    with open('config.wtc', 'w', encoding='utf-8') as f:
        f.write(data)
        f.close()

new_config = """server=192.367.192
bpp=16
turnoffmenu=always, msg: "Aguardando tecla..."
disk=usb
keyboard=pt-br
connection"""

def post_new_config():
    URLFile = "https://t.wtware.local/upload_config_file"
    with requests.Session() as s:

        create_wtc(new_config)               
        new_config_file = {"file1":('config.wtc',open('config.wtc','rb'))}

        try:            
            s.post(URLFile, auth=('', '123456'), files=new_config_file,verify=False)
        except HTTPError as e:
            if e.code == 404:
                print("I'm a teapot")
        else:
            get_and_print_config('t.wtware.local')
            
def get_and_print_config(ip):
    with requests.Session() as s:
        r = s.get(f"https://{ip}/config_edit", auth=('', '123456'), verify=False)        
        soup = BeautifulSoup(r.text, 'html.parser')
        config = soup.findAll('textarea')
        return print(config)
        
if __name__ == "__main__":
    post_new_config()      
    