Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

socket programming python

"""
UDP echo server that converts a message
received from client into uppercase and
then sends it back to client. 
"""
from socket import *
# Port number of server
server_port = 12000
# Server using IPv4 and UDP socket
server_socket = socket(AF_INET, SOCK_DGRAM)
# Bind server to port number and IP address
server_socket.bind(("127.0.0.1", server_port))
print("The server is ready to receive msg...")
while True:
    # Extract message and client address from received msg
    message, client_address = server_socket.recvfrom(2048)
    # Create response message
    modified_message = message.upper()
    server_socket.sendto(modified_message, client_address)
Comment

socket programming python

from socket import *
# Set server name and port number
server_name = "localhost"
server_port = 12000
# Create a UDP socket using IPv4
client_socket = socket(AF_INET, SOCK_DGRAM)
# Get message to send to server
message = input("Input sentence to send to server:")
# Convert it into bytes
message_bytes = bytes(message, encoding='utf-8')
client_socket.sendto(message_bytes, (server_name, server_port))
# Wait for server's response
modified_message, server_address = client_socket.recvfrom(2048)
# Display it on screen
print(modified_message.decode("utf-8"))
client_socket.close()
Comment

web socket in python

#1) Installation
#Go to your cmd and type pip install websockets

#2) Utilisation 
#Here’s how a client sends and receives messages:

import asyncio
import websockets

async def hello():
    async with websockets.connect("ws://localhost:8765") as websocket:
        await websocket.send("Hello world!")
        await websocket.recv()

asyncio.run(hello())

#And here’s an echo server:

import asyncio
import websockets

async def echo(websocket):
    async for message in websocket:
        await websocket.send(message)

async def main():
    async with websockets.serve(echo, "localhost", 8765):
        await asyncio.Future()  # run forever

asyncio.run(main())
Comment

PREVIOUS NEXT
Code Example
Python :: casting in python 
Python :: how to set the size of a kivy window bigger than screen 
Python :: nth root of a number python 
Python :: blur an image in python 
Python :: How to Connect Google Colab to a Local Jupyter Runtime 
Python :: array in python 
Python :: SUMOFPROD1 
Python :: python does string contain space 
Python :: how to print 2 list in python as table 
Python :: python search in json file 
Python :: HTML template with Django email 
Python :: plt.tight_layout() cuts x axis 
Python :: pop element from heap python 
Python :: request post python 
Python :: pandas compare two columns of different dataframe 
Python :: python tkinter menu widget 
Python :: embed variables python 
Python :: run python script inside bash script 
Python :: combining strings in python 
Python :: convert price to float pandas 
Python :: bot delete embed py 
Python :: dictionary comprehension python 
Python :: interpreter vs compiler 
Python :: python api request 
Python :: python linux script 
Python :: add data to empty column pandas 
Python :: pip config proxy 
Python :: django add to database 
Python :: function to perform pairs bootstrap estimates on linear regression parameters 
Python :: Python Print Variable Using the string formatting with positional arguments {} 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =