0

hey guys I been trying to host a local server on port 22222 and when someone open it on the browser I get a msgbox saying yes / no - if you click yes it will accept the connection and show index.html basically, if you click no it will return nothing or something, anyway it works perfectly BUT

I'm trying to trigger a second msgbox after you click on the first one with a delay(after command), when you access the first time and click yes, it serves the page but the Established msg does not show up, if you refresh the page than it retriggers the Established msg and than ask again if you want to accept the connection (Bug Alert lol)

basiclly you need to run this code open 127.0.0.1:22222, click yes, than you will not see the its_ok msgbox, unless you do all this again

import sys
from http.server import HTTPServer, SimpleHTTPRequestHandler, test as Brain_Link
import tkinter
import tkinter.messagebox as mbox
window = tkinter.Tk()
window.wm_withdraw()
window.attributes("-topmost", True)

def Start_Brain_Link(*args):
    Brain_Link(*args, port=22222)

def its_ok():
    mbox.showinfo('A.I','Brain-Link Established!')

def its_bad():
    mbox.showinfo('A.I','Brain-Link Attemp Blocked!')

class CORSRequestHandler(SimpleHTTPRequestHandler):
    def end_headers (self):
        if mbox.askquestion("Warning!", self.client_address[0]+" Requested Brain-Link!", icon='warning') == 'yes':
                self.send_header('Access-Control-Allow-Origin', '*')
                SimpleHTTPRequestHandler.end_headers(self)
                window.after(1000,its_ok)
        else:
                window.after(1000,its_bad)

if __name__ == '__main__':
    Start_Brain_Link(CORSRequestHandler, HTTPServer)
orr burgel
  • 381
  • 1
  • 6
  • 16
  • bonus for someone who want to understand whats behind this script logic: (HTML FILE) https://pastebin.com/NNmYwuYZ – orr burgel Aug 12 '18 at 20:40
  • You can't use Tkinter from a background thread. There are multiple workarounds, but they're all clunky, and you have to decide which one you want. Just knowing that you've looked at some existing answers without knowing which ones you looked at and what you don't understand, there's really nothing else anyone can tell you that wouldn't just be a repeat of one of those answers you already didn't understand. – abarnert Aug 12 '18 at 20:40
  • Also, please don't post your code on an offsite link; post a [mcve] as part of the text of your question. – abarnert Aug 12 '18 at 20:41
  • Ok I removed my offsite link sorry for that, and yes that's really a problem to answer such a question in such a way that everyone will understand since the code is always unique, but I will sure appreciate if you try to explain and maybe someone will do put use for it who knows... – orr burgel Aug 12 '18 at 20:44
  • I can write an answer showing, e.g., how to build a queue and pass messages from the background thread and check it each time through the event loop on the main thread, but it would be identical to the other answers on this site showing the same thing. If those answers didn't help you, this wouldn't help you either. You need to explain why they didn't help you if you want something more explained to you. – abarnert Aug 12 '18 at 20:46
  • I read alot I don't wanna start giving lots of examples and backlinks to them can you please send a link to a post you think will help me and I will read and try a few times before I report back here if it worked, sorry for the trouble highly appreciate it. – orr burgel Aug 12 '18 at 20:50
  • [This question](https://stackoverflow.com/questions/3567238/) explains things well, and has a link to the Tkinter Book article with an extensive and clear example (although that example has to be updated from Python 2.4). – abarnert Aug 12 '18 at 20:54
  • [This one](https://stackoverflow.com/questions/51729665/) shows how to not use threads in the first place, which I think is what you probably want. (A messagebox is modal and blocks the entire GUI anyway, after all.) Here's [an answer](https://stackoverflow.com/questions/51274493/) that tries to cover both. – abarnert Aug 12 '18 at 20:56
  • the message box actually is not the issue, lets say you skip it and replace it with exit command It still doesn't work like it only exit the thread and not the main thread, and if I put exit() without the threading than the GET request is not even sent... frustrating – orr burgel Aug 12 '18 at 21:01
  • I believe there must be some sort of command that I'm missing that will pass the GET request and than show the msgbox with no threading at all, cant figure it out tho, will read your non threaded post and report (tried self.finish and the one that send 200 request and all that still request is being cancelled by the exit command that I put where I call the threaded its_ok) – orr burgel Aug 12 '18 at 21:03
  • please read my update, its based on This one https://stackoverflow.com/questions/51729665/ no errors, but a weird bug (you have to enter 127.0.0.1:22222 twice to see the second msgbox) – orr burgel Aug 12 '18 at 21:16
  • anyone can help me with my new problem please? should I repost? – orr burgel Aug 12 '18 at 22:56
  • Post a new question for a new problem – Reblochon Masque Aug 13 '18 at 02:07

0 Answers0