Skip to content

Python code snippet – How to host a local server?

________________________________________________________________________________



--------------------------------------------------------------------------------
THIS IS HOW TO HOST THE SERVER :
--------------------------------------------------------------------------------
import time
import sys
import socket
import os

s = socket.socket()
host = socket.gethostname()
print(host)
port = 8080
s.bind((host,port))
print("")
print("Waiting for a device to connect...")
print("")
s.listen(5)
conn, addr = s.accept()
print (addr, " - is connected")
print ("")





--------------------------------------------------------------------------------
THIS IS HOW TO MAKE THE CLIENT FOR THE SERVER :
--------------------------------------------------------------------------------
import time
import sys
import socket
import os

s = socket.socket()
host = input("Type here hostname : ")
port = 8080
s.connect((host,port))
print("")
print("connected to server")
print("")



________________________________________________________________________________


See also  Python code snippet - How to check if a list raises IndexError but wihing a if statement ?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.