- Code: Select all
import socket
import ssl
from http.server import BaseHTTPRequestHandler,HTTPServer
def send_request(myself, req):
if(req == '/favicon.ico'):
print('favicon.ico')
return
station = req[4:]
hostname = 'tgftp.nws.noaa.gov'
protocol = 'GET http://'
page = protocol + hostname + station + ' HTTP/1.1\r\nHost: ' +hostname + '\r\nConnection: close\r\nContent-Type: text/plain\r\n\r\n'
context = ssl.create_default_context()
print(page)
with socket.create_connection((hostname, 443)) as sock:
with context.wrap_socket(sock, server_hostname=hostname) as ssock:
ssock.sendall(page.encode())
while(1):
receivedMsg = ssock.recv(1024)
# print(receivedMsg)
if(len(receivedMsg) == 0):
ssock.close()
break
myself.wfile.write(receivedMsg)
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
result = send_request(self,self.path)
#----------------------------------------------------------------------
if __name__ == '__main__':
serverAddress = ('127.0.0.1', 8080)
server = HTTPServer(serverAddress, RequestHandler)
server.serve_forever()
I saved it as server.py.
Run it with
- Code: Select all
python3 server.py
Use this link in a web browser to test it. I used Firefox.
http://127.0.0.1:8080/////data/observat ... s/KSFO.TXT
Note 5 slashes after 8080. It should display KSFO weather. If so, it is working.
BEFORE YOU GO ANY FURTHER: Copy your original fgfs executable out of harm's way as a backup in case of problems.
Edit the fgfs executable with a binary (HEX) editor, I used ghex from the Ubuntu repository.
Search for "http://t". You will find this string:
http://tgftp.nws.noaa.gov/data/observat ... /stations/
Edit it to this:
http://127.0.0.1:8080/////data/observat ... /stations/
Ensure you don't insert new text. Overwrite only. Note the five slashes. Don't worry. The server.py code removes them.
Save.
I guarantee nothing. It works for me on Ubuntu 18.04.