lol maybe working
This commit is contained in:
parent
0d9252bb5d
commit
7a9381e0a7
36
good_client.py
Normal file
36
good_client.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
import requests
|
||||
|
||||
servers = {
|
||||
"anthony": 'http://10.29.55.48',
|
||||
"alek": 'http://10.29.72.197',
|
||||
"2w": 'http://10.242.5.239'
|
||||
}
|
||||
|
||||
uname = input("what is your uname?\n>")
|
||||
|
||||
print(servers)
|
||||
server_id = input("what server would you like to connect to?\n>")
|
||||
|
||||
port = 8000
|
||||
|
||||
SERVER = f"{servers[server_id]}:{port}"
|
||||
print(f"Connecting to {SERVER}")
|
||||
requests.post(f"{SERVER}/connect", params={"uname": uname})
|
||||
|
||||
while True:
|
||||
choice = input(f"what would you like to do {uname}?\n>>> ")
|
||||
|
||||
if choice == "get":
|
||||
url = f"{SERVER}/getmsg?uname={uname}"
|
||||
x = requests.get(url)
|
||||
print(x.text)
|
||||
elif choice == "send":
|
||||
who = input("to whom ?\n> ")
|
||||
what = input("what to send then?\n>")
|
||||
url = f"{SERVER}/sendmsg?recipient={who}&data={what}"
|
||||
x = requests.post(url)
|
||||
print(f"POSTING to {url}")
|
||||
print(x.text)
|
||||
else:
|
||||
print("all i can do is [get] / [send] ")
|
||||
|
88
good_server.py
Normal file
88
good_server.py
Normal file
|
@ -0,0 +1,88 @@
|
|||
from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler
|
||||
import json
|
||||
from urllib import parse
|
||||
import requests
|
||||
|
||||
inboxes = {}
|
||||
connected_clients = []
|
||||
all_servers = ['http://10.29.55.48:8000', 'http://10.29.72.197:8000', 'http://10.242.5.239:8000']
|
||||
|
||||
def Connect(uname):
|
||||
connected_clients.append(uname)
|
||||
inboxes[uname] = []
|
||||
|
||||
def GetMsg(uname):
|
||||
if uname in connected_clients:
|
||||
return inboxes[uname]
|
||||
else:
|
||||
return f"{uname} not connected to this server!"
|
||||
|
||||
def Search(client_address, uname):
|
||||
for friend_server in all_servers:
|
||||
x = requests.get(f"{friend_server}/search", params={"uname": uname})
|
||||
if x.text != "no":
|
||||
return friend_server
|
||||
return "no"
|
||||
|
||||
def RecordMsg(recipient, data):
|
||||
if recipient in connected_clients:
|
||||
inboxes[recipient].append(data)
|
||||
else:
|
||||
return False
|
||||
|
||||
def SendMsg(recipient, data):
|
||||
location = Search(MYIP, recipient)
|
||||
requests.post(f"{location}/recordmsg?recipient={recipient}&data={data}")
|
||||
|
||||
class HTTPRequestHandler(BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
self.send_response(200)
|
||||
self.send_header("Content-type", "text/html")
|
||||
self.end_headers()
|
||||
|
||||
request_path = parse.urlsplit(self.path).path
|
||||
request_args = parse.parse_qs(parse.urlsplit(self.path).query)
|
||||
|
||||
print(request_path)
|
||||
|
||||
if request_path == "/getmsg":
|
||||
print("/getmsg")
|
||||
self.wfile.write(str(GetMsg(request_args["uname"][0])).encode('utf-8'))
|
||||
elif request_path == "/search":
|
||||
print("/search", MYIP)
|
||||
self.wfile.write(str(Search(self.client_address[0]+":8000", request_args["uname"][0])).encode('utf-8'))
|
||||
else:
|
||||
self.wfile.write(str("hmmmmm").encode('utf-8'))
|
||||
|
||||
def do_POST(self):
|
||||
request_path = parse.urlsplit(self.path).path
|
||||
request_args = parse.parse_qs(parse.urlsplit(self.path).query)
|
||||
|
||||
print(request_path)
|
||||
|
||||
if request_path == "/sendmsg":
|
||||
SendMsg(request_args["recipient"][0], request_args["data"][0])
|
||||
elif request_path == "/recordmsg":
|
||||
RecordMsg(request_args["recipient"][0], request_args["data"][0])
|
||||
elif request_path == "/inform":
|
||||
Inform(request_args["uname"][0], request_args["location"][0])
|
||||
elif request_path == "/connect":
|
||||
Connect(request_args["uname"][0])
|
||||
print(connected_clients)
|
||||
|
||||
print("END")
|
||||
|
||||
self.send_response(200)
|
||||
self.end_headers()
|
||||
|
||||
# port = int(input("what port do you wanna run the server on? 8000 is a pretty good choice\n> ").strip())
|
||||
port = 8000
|
||||
|
||||
import socket
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
s.connect(("8.8.8.8", 80))
|
||||
MYIP = f"http://{s.getsockname()[0]}:{port}"
|
||||
|
||||
server = ThreadingHTTPServer(("0.0.0.0", port), HTTPRequestHandler)
|
||||
server.serve_forever()
|
||||
|
Loading…
Reference in a new issue