How to serve my computer as a host using Python?

Viewed 34

I'm trying to get my website to communicate with my Python server located on my computer. I have directed my website link to my computer's public IP 58.186.28.11 and I built my server like this:

from Server.server import *
import socket
import requests

if __name__ == '__main__':
    PATH = requests.get('https://checkip.amazonaws.com').text.strip() # Get public IP
    PORT = 8080
    print('------------------------------------------------------------')
    print("Connect to '{}:{}/' to access the website.".format(str(PATH), str(PORT)))
    print('------------------------------------------------------------\n')

    httpd = HTTPServer((PATH, PORT), Server)
    httpd.serve_forever()

But the server throws this error:

Traceback (most recent call last):
  File "c:\Users\ngthi\OneDrive\Máy tính\NMCNPM\Thực hành\Đồ án\Coding\main.py", line 17, in <module>       
    httpd = HTTPServer((PATH, PORT), Server)
  File "C:\Users\ngthi\AppData\Local\Programs\Python\Python39\lib\socketserver.py", line 452, in __init__   
    self.server_bind()
  File "C:\Users\ngthi\AppData\Local\Programs\Python\Python39\lib\http\server.py", line 138, in server_bind 
    socketserver.TCPServer.server_bind(self)
  File "C:\Users\ngthi\AppData\Local\Programs\Python\Python39\lib\socketserver.py", line 466, in server_bind
    self.socket.bind(self.server_address)
OSError: [WinError 10049] The requested address is not valid in its context

Is there any way to allow http://example.com to communicate with my built server?

0 Answers
Related