“Programowanie gniazd Python” Kod odpowiedzi

Wyślij dane za pośrednictwem gniazda TCP Python

import socket
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 1024
MESSAGE = "Hello, World!"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(MESSAGE)
data = s.recv(BUFFER_SIZE)
s.close()
print "received data:", data
Glamorous Gerenuk

Programowanie sieci Python

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.
s.bind((host, port))        # Bind to the port

s.listen(5)                 # Now wait for client connection.
while True:
   c, addr = s.accept()     # Establish connection with client.
   print 'Got connection from', addr
   c.send('Thank you for connecting')
   c.close()                # Close the connection
Casual Coder

Programowanie gniazd 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()
Wissam

Programowanie gniazd 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)
Wissam

Gniazdo internetowe w 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())
moi_crn

Odpowiedzi podobne do “Programowanie gniazd Python”

Pytania podobne do “Programowanie gniazd Python”

Więcej pokrewnych odpowiedzi na “Programowanie gniazd Python” w Python

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu