How do I connect to the internet through client's wifi? | Python - python

I've set up my server.py and client.py with the socket module, is there a way you can connect to the internet through the client? What i mean is, I want to have the server connect to the client through it's own internet, and then from there have the client use its internet to browse the web. But I have no idea how to do this.
So is there something I can leverage to achiece this?
Server:
import socket, threading
from time import sleep
PORT = 5430
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
def handle_client(conn, addr):
print(f'[NEW CONNECTIONS] {addr} connected')
while True:
conn.recv(6000)
conn.close()
def start():
server.listen()
print(f'[LISTENING] Server is listening on {SERVER}')
while True:
conn, addr = server.accept()
thread = threading.Thread(target = handle_client, args = (conn,addr))
thread.start()
print(f'[ACTIVE CONNECTIONS] {threading.activeCount() - 1}')
print('[STARTNG] Server is starting')
start()
Client:
from time import sleep
import socket
PORT = 5430
SERVER = socket.gethostbyname(socket.gethostname())
FORMAT = 'utf-8'
ADDR = (SERVER, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
while True:
client.send('hi').encode(FORMAT)
conn.close()

Related

How to manually shutdown a socket server?

I have a simple socket server, how do I shut it down when I enter "shutdown" in the terminal on the server side?
import socket
SERVER = "xxxx"
PORT = 1234
ADDR = (SERVER, PORT)
FORMAT = "utf-8"
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
def handle_connection(conn, addr):
...
server.listen()
while True:
conn, addr = server.accept()
handle_connection(conn, addr)
Close active connections and exit. It can be done with:
server.close()
exit(0)
To shutdown you socket server manually by calling server.close(), you whole code should be:
import socket
SERVER = "xxxx"
PORT = 1234
ADDR = (SERVER, PORT)
FORMAT = "utf-8"
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
def handle_connection(conn, addr):
...
server.listen()
while True:
conn, addr = server.accept()
handle_connection(conn, addr)
# call server.close() to shut down your server.

How can I have an admin-client to remote shutdown server.py in a socket programming multiple clients?

So can someone please tell me how to have an admin-client shutting down the Server (server.py) in a socket multiple clients architecture? I want admin-client to type "shutdown" in client side then server will be shutdown. and right after submit, the server will call a function that shows network load graph . a graph with the number of requests per time slot.
Server:
`
import socket, threading
class ClientThread(threading.Thread):
def __init__(self,clientAddress,clientsocket):
threading.Thread.__init__(self)
self.csocket = clientsocket
print ("New connection added: ", clientAddress)
def run(self):
print ("Connection from : ", clientAddress)
#self.csocket.send(bytes("Hi, This is from Server..",'utf-8'))
msg = ''
while True:
data = self.csocket.recv(2048)
msg = data.decode()
if msg=='bye':
break
print ("from client", msg)
self.csocket.send(bytes(msg,'UTF-8'))
print ("Client at ", clientAddress , " disconnected...")
LOCALHOST = "127.0.0.1"
PORT = 8080
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind((LOCALHOST, PORT))
print("Server started")
print("Waiting for client request..")
while True:
server.listen(1)
clientsock, clientAddress = server.accept()
newthread = ClientThread(clientAddress, clientsock)
newthread.start()
Client:
import socket
SERVER = "127.0.0.1"
PORT = 8080
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((SERVER, PORT))
client.sendall(bytes("This is from Client",'UTF-8'))
while True:
in_data = client.recv(1024)
print("From Server :" ,in_data.decode())
out_data = input()
client.sendall(bytes(out_data,'UTF-8'))
if out_data=='bye':
break
client.close()
`
I have tried
if message == "shutdown":
close()
exit(0)
but dont know how to apply it

How to enable a python multithreading server to run on localhost and receive html files?

I'm currently working on a multithreaded web server in Python which should be capable of processing HTTP requests sent from a browser or any other client programs.
I have a client.py file and a server.py file. Right now, I can run both of them on separate terminals and send messages from the client to the server.
What I want to do is to host the server on a localhost and send HTML files from the client to the server, so that the HTML files can be displayed on the server - hence on the localhost site. The server and client codes are down below. Help would be highly appreciated.
Thanks in advance!
Server.py -
import socket
import threading
IP = socket.gethostbyname(socket.gethostname())
PORT = 5566
ADDR = (IP, PORT)
SIZE = 1024
FORMAT = "utf-8"
DISCONNECT_MSG = "!DISCONNECT"
def handle_client(conn, addr):
print(f"[NEW CONNECTION] {addr} connected.")
connected = True
while connected:
msg = conn.recv(SIZE).decode(FORMAT)
if msg == DISCONNECT_MSG:
connected = False
print(f"[{addr}] {msg}")
msg = f"Msg received: {msg}"
conn.send(msg.encode(FORMAT))
conn.close()
print("Server is starting...")
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
server.listen()
print(f"Server is listening on {IP}:{PORT}")
while True:
conn, addr = server.accept()
try:
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
print(f"[ACTIVE CONNECTIONS] {threading.active_count() - 1}")
except IOError:
conn.send('\nHTTP/1.1 404 Not Found\n\n'.encode())
conn.close()
Client.py -
import socket
IP = socket.gethostbyname(socket.gethostname())
PORT = 5566
ADDR = (IP, PORT)
SIZE = 1024
FORMAT = "utf-8"
DISCONNECT_MSG = "!DISCONNECT"
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
print(f"[CONNECTED] Client connected to server at {IP}:{PORT}")
connected = True
while connected:
msg = input("> ")
client.send(msg.encode(FORMAT))
if msg == DISCONNECT_MSG:
connected = False
else:
msg = client.recv(SIZE).decode(FORMAT)
print(f"[SERVER] {msg}")

Socket server keeps listening but no response is received

I have the following code, and with any valid address or port, the code keeps listening, but either doesn't receive any response or doesn't print it.
import socket, threading
bind_ip = "127.0.0.1"
bind_port = 39832
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip, bind_port))
server.listen(5)
print(f"[*] Listening on {bind_ip}: {bind_port}")
def handle_client(client_socket):
response = client_socket.recv(1024)
print(f"[*] Recieved: {response}")
client_socket.send("ACK!")
client_socket.close()
while True:
client, addr = server.accept()
print(f"[*] Accepted connection from: {addr[0]}: {addr[1]}")
client_handler = threading.Thread(target=handle_client, args=(client,))
client_handler.start()
I can't find the problem as the book I am reading uses the same code but receives a response.

Python - how to stop socket recv() waiting if there is no coming data sent from client

I'm leanring how sockets work and trying to do some simple things.
My client is NOT sending anything to the server, and the server will not receive anything. But the problem is that the server socket will be always waiting for nothing. I want it to do something else if there is no available coming data from the client side. The if statement does not help end its waiting.
Server.py:
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
hostname = socket.gethostname()
host = socket.gethostbyname(hostname)
port = 9090
server.bind((host, port))
server.listen(10)
con, addr = server.accept()
msg = con.recv(2048)
if not msg:
con.send('hello world'.encode('utf-8'))
con.close()
server.close()
else:
con.send('hi Client I received it'.encode('utf-8'))
con.close()
server.close()
Client.py:
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('192.155.11.79', 9090))
data = client.recv(2048).decode('utf-8')
print('From server side: ', data)
client.close()

Categories

Resources