-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_Socket_Programming_Socket_Server_Client_Side.py
More file actions
33 lines (24 loc) · 1.27 KB
/
Copy pathPython_Socket_Programming_Socket_Server_Client_Side.py
File metadata and controls
33 lines (24 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Python Socket Programming.
# Of the various forms of Inter Process Communication (IPC), sockets are by far the most popular.
# The combination of sockets with INET makes talking to arbitrary machines around the world unbelievably
# easy.
# The client application (your browser, for example) uses “client” sockets for client-server communication;
# the web server it’s talking to uses both “server” sockets and “client” sockets.
# socketserver — A framework for network servers
# The difference is that the readline() call in the second handler will call recv() multiple times until it
# encounters a newline character, while the single recv() call in the first handler will just return what has
# been sent from the client in one sendall() call.
# Client Side:
import socket
import sys
HOST, PORT = "localhost", 9999
data = " ".join(sys.argv[1:])
# Create a socket (SOCK_STREAM means a TCP socket)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
# Connect to server and send data
sock.connect((HOST, PORT))
sock.sendall(bytes(data + "\n", "utf-8"))
# Receive data from the server and shut down
received = str(sock.recv(1024), "utf-8")
print("Sent: {}".format(data))
print("Received: {}".format(received))