Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python server

python -m http.server 8080
Comment

server on python

import http.server
import socketserver

PORT = 8000

Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()
Comment

Python Server Code

import socket 
import threading

HEADER = 64
PORT = 5050
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)

def handle_client(conn, addr):
    print(f"[NEW CONNECTION] {addr} connected.")

    connected = True
    while connected:
        msg_length = conn.recv(HEADER).decode(FORMAT)
        if msg_length:
            msg_length = int(msg_length)
            msg = conn.recv(msg_length).decode(FORMAT)
            if msg == DISCONNECT_MESSAGE:
                connected = False

            print(f"[{addr}] {msg}")
            conn.send("Msg received".encode(FORMAT))

    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("[STARTING] server is starting...")
start()
Comment

python server

python3 -m http.server [port]
Comment

server in python

def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler):
    server_address = ('', 8000)
    httpd = server_class(server_address, handler_class)
    httpd.serve_forever()
Comment

PREVIOUS NEXT
Code Example
Python :: PHP echo multi lines Using Nowdoc variable 
Python :: pytorch convert tensor dtype 
Python :: python count of values in array 
Python :: with torch.no_grad() 
Python :: python online practice test 
Python :: python bild speichern 
Python :: slicing strings in python 
Python :: python logging level 
Python :: count number of element in list 
Python :: python warnings as error 
Python :: how to convert categorical data to numerical data in python 
Python :: change date format to yyyy mm dd in django template datepicker 
Python :: Django serializer, 
Python :: 1*2*3*4*5*6* - print on console?by python 
Python :: journalctl not showing all python prints 
Python :: Chudnovsky algorithm in python codes 
Python :: dockerize django app 
Python :: telegram.ext module python 
Python :: python scipy put more weight to a set value in curve_fit 
Python :: for loop to select rows in pandas 
Python :: python power 
Python :: get admin url of instance django 
Python :: Change Separator Value When Printing 
Python :: get nonzero min numpy 
Python :: django get current user in form 
Python :: entry tkinter 
Python :: 9x9 grid tkinter 
Python :: python nearly equal 
Python :: inicio programacao python 
Python :: __add__ 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =