Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python how to make a server

#server
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 1234))#the 1234 is the server ip that client is going to connect to.
s.listen(5)


while True:
    clientsocket, Address = s.accept()
    print(f'IP Address [{Address}] has connect to the server')#this is what the server will show when the client connect to the server.
    clientsocket.send(bytes("hi", "utf-8" ))#this is what the client will be showen.
    clientsocket.close()
#---------------------------------------------------------------
#client
#this code needs to be in a diffenet file
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((socket.gethostname(), 1234))#the 1234 is the server ip the client is connecting to.

full_msg = ''

while True:
    msg = s.recv(8)
    if len(msg) <= 0:
        break
    full_msg += msg.decode("utf-8")
    print(msg.decode("utf-8"))
Comment

how to create a python server

py -m http.server
localhost:8000
Comment

PREVIOUS NEXT
Code Example
Python :: access row of dataframe 
Python :: get dictionary value python 
Python :: zero crossing rate python 
Python :: generate dates between two dates python 
Python :: import antigravity in python 
Python :: groupby and sort python 
Python :: bitcoin wallet python 
Python :: pandas average every n rows 
Python :: python how to draw triangle 
Python :: read json in python 
Python :: circular list python 
Python :: python array extend 
Python :: show all rows for dataframe 
Python :: throw error in python 
Python :: how to get input with python 
Python :: add place in certain index python string 
Python :: get a list as input from user 
Python :: python - count total numeber of row in a dataframe 
Python :: create a dictionary from a list python 
Python :: float infinity python 
Python :: encrypt string with key python 
Python :: python turtle triangle 
Python :: matplotlib python background color 
Python :: roman to integer 
Python :: python to mac executable 
Python :: make the first letter of a string upper case 
Python :: separating tuple in pandas 
Python :: pydrive upload file to folder 
Python :: python text input 
Python :: Python NumPy swapaxis Function Example 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =