1import socket
2TCP_IP = '127.0.0.1'
3TCP_PORT = 5005
4BUFFER_SIZE = 1024
5MESSAGE = "Hello, World!"
6s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7s.connect((TCP_IP, TCP_PORT))
8s.send(MESSAGE)
9data = s.recv(BUFFER_SIZE)
10s.close()
11print "received data:", data
1#!/usr/bin/env python3
2
3import socket
4
5HOST = '127.0.0.1' # The server's hostname or IP address
6PORT = 65432 # The port used by the server
7
8with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
9 s.connect((HOST, PORT))
10 s.sendall(b'Hello, world')
11 data = s.recv(1024)
12
13print('Received', repr(data))
14
1#!/usr/bin/env python3
2
3import socket
4
5HOST = '127.0.0.1' # Standard loopback interface address (localhost)
6PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
7
8with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
9 s.bind((HOST, PORT))
10 s.listen()
11 conn, addr = s.accept()
12 with conn:
13 print('Connected by', addr)
14 while True:
15 data = conn.recv(1024)
16 if not data:
17 break
18 conn.sendall(data)
19
1import socket # Import socket module
2
3s = socket.socket() # Create a socket object
4host = socket.gethostname() # Get local machine name
5port = 12345 # Reserve a port for your service.
6s.bind((host, port)) # Bind to the port
7
8s.listen(5) # Now wait for client connection.
9while True:
10 c, addr = s.accept() # Establish connection with client.
11 print 'Got connection from', addr
12 c.send('Thank you for connecting')
13 c.close() # Close the connection
1from socket import *
2import sys
3import json
4
5
6serverName = ""
7serverPort = 6100
8
9
10clientSocket = socket(AF_INET, SOCK_STREAM)
11clientSocket.connect((serverName, serverPort))
12
13sample_data = {
14 "Aparna" : 1,
15 "Pooja" : 2,
16 "Shreya" : 3,
17 "Tanishq" : 4
18}
19
20serialized_data = json.dumps(sample_data) #data serialized
21
22# clientSocket.send(str.encode(sample_data))
23clientSocket.send(str.encode(serialized_data))
24
25response_data = clientSocket.recv(1024)
26print("Response data from server : ", response_data.decode())
27
28clientSocket.close()
1import socket
2
3HOST = '127.0.0.1' # Standard loopback interface address (localhost)
4PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
5
6with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
7 s.bind((HOST, PORT))
8 s.listen()
9 conn, addr = s.accept()
10 with conn:
11 print('Connected by', addr)
12 while True:
13 data = conn.recv(1024)
14 if not data:
15 break
16 conn.sendall(data)