Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python sockets

'''
a library used to create end points. 
heres a quick exemple of a client and a local TCP server:'''

#server file
import socket

soc = socket.socket(socket.AF_INET) #initializing the socket

IP = "111.111.1.111" #the ip of the server. use 'ipconfig' on cmd to check your ip address (make sure its ipv4 because its a local server)
PORT = 5050 #the port of the server. can be any port as long its not in use 

soc.bind((IP,PORT)) #binding the ip and the port of the server
soc.listen() #letting the server accept clients
client, address = soc.accept() #waiting for a user to connect and store the user in the client variable and them address to the adress variable
client.send(b'hello client!') #sending a byte string to the client.



#client file (can be created from a different pc): 

soc = socket.socket(socket.AF_INET) #initializing the socket

IP = "111.111.1.111" #the ip of the server.  
PORT = 5050 #the port of the server.

soc.connect((IP,PORT)) #connecting to the server.

print(soc.recv(1024).decode()) #receiving the byte string from the server. passing as a parameter how much bytes we want to recieve. and we printing it on the string (the .decode() is used to decode the bytes)
Comment

socketserver 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

PREVIOUS NEXT
Code Example
Python :: python arrays 
Python :: django serializer 
Python :: sklearn classifiers 
Python :: negative number factor python 
Python :: pandas isin 
Python :: python get element from dictionary 
Python :: django admin 
Python :: python tkinter grid 
Python :: map example in python 
Python :: planet 
Python :: python list all but first 
Python :: import yaml python3 
Python :: adding text cv2 
Python :: Create a single executable from a Python project 
Python :: rotate matrix python 
Python :: from django.http import HttpResponse 
Python :: input pythhon 
Python :: machine learning python 
Python :: can only concatenate str (not "int") to str 
Python :: how to convert binary to integer in python 
Python :: python remove empty values from list 
Python :: how to make a python file that prints out a random element from a list 
Python :: get definition of word python 
Python :: how to create a loading in pyqt5 
Python :: one hot numpy 
Python :: mechanize python 
Python :: create dict from two lists 
Python :: spotify api python 
Python :: selenium click on item in a list 
Python :: check setuptools version python 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =