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 :: django authenticate 
Python :: add to python list 
Python :: loop through words in a string python 
Python :: django textfield 
Python :: discord.py get server id 
Python :: flask login 
Python :: python import graphviz 
Python :: python 1 line for loop with else 
Python :: from collections import defaultdict 
Python :: python remove blanks from string 
Python :: pandas merge two dataframes remove duplicates 
Python :: selenium set chrome executable path 
Python :: pass context data with templateview in django 
Python :: what is *args and **kwargs in django 
Python :: django createssuperuser 
Python :: python json string indices must be integers 
Python :: python list add element to front 
Python :: count elements in columns pandas 
Python :: generate random integers 
Python :: how to set and run flask app on terminal 
Python :: Make Rotation matrix in Python 
Python :: how to reindex columns in pandas 
Python :: remove specific character from object in pandas column using iloc 
Python :: python dictionary 
Python :: python file to array 
Python :: plot title overlapping yaxis python 
Python :: hash() python 
Python :: 405 status code django 
Python :: python convert json string to class 
Python :: Write Python programs to print numbers from 1 to 10000 while loops 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =