mirror of
https://github.com/metafy-social/python-scripts.git
synced 2024-11-27 14:01:12 +00:00
Add socket programming scripts
This commit is contained in:
parent
d080931b09
commit
76b0c6beba
16
scripts/Socket_Programming_Python/README.md
Normal file
16
scripts/Socket_Programming_Python/README.md
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
# Socket Progamming in Python
|
||||||
|
|
||||||
|
We all know how to use python for basic stuff and general programming. But this script allows you to create a server and client of your own using sockets.
|
||||||
|
|
||||||
|
# What are sockets?
|
||||||
|
|
||||||
|
A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to. An endpoint is a combination of an IP address and a port number.
|
||||||
|
|
||||||
|
|
||||||
|
# Requirments
|
||||||
|
|
||||||
|
Clone Repo
|
||||||
|
You don't need to install anything just import sockets library.
|
||||||
|
More information here : https://docs.python.org/3/howto/sockets.html
|
||||||
|
Use server.py for server
|
||||||
|
Use client.py for client
|
9
scripts/Socket_Programming_Python/client1.py
Normal file
9
scripts/Socket_Programming_Python/client1.py
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
|
||||||
|
import socket
|
||||||
|
|
||||||
|
client_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||||
|
port = 1024
|
||||||
|
host = socket.gethostbyname(socket.gethostname()) # get the hostname
|
||||||
|
client_socket.connect((host, port))
|
||||||
|
message = client_socket.recv(2048)
|
||||||
|
print(message.decode("utf-8"))
|
11
scripts/Socket_Programming_Python/server1.py
Normal file
11
scripts/Socket_Programming_Python/server1.py
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
import socket
|
||||||
|
|
||||||
|
server_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||||
|
port = 1024
|
||||||
|
host = socket.gethostbyname(socket.gethostname()) # get the hostname
|
||||||
|
server_socket.bind((host, port))
|
||||||
|
server_socket.listen(2)
|
||||||
|
while True:
|
||||||
|
conn, address = server_socket.accept()
|
||||||
|
print("Connection from: " + str(address))
|
||||||
|
conn.send(bytes("Socket programming in python","utf-8"))
|
Loading…
Reference in New Issue
Block a user