Python Basic Socket network Demo

Published: 18 March 2015
on channel: Leigh
1,013
2

Just testing the "socket" module for networking in Python



server.py

import time
import socket

creating a socket object
s = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)

get local Host machine name
host = socket.gethostname()
port = 9999

bind to pot
s.bind((host, port))

que up to 5 requests
s.listen(5)

while True:
establish connection
clientSocket, addr = s.accept()
print("got a connection from %s" % str(addr))
currentTime = time.ctime(time.time()) + "\r\n"
clientSocket.send(currentTime.encode('ascii'))
clientSocket.close()


client.py

import socket

s = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)

host = socket.gethostname()
port = 9999

s.connect((host, port))

tm = s.recv(1024)

s.close()
print("the time we got from the server is %s" % tm.decode('ascii'))


On this page of the site you can watch the video online Python Basic Socket network Demo with a duration of hours minute second in good quality, which was uploaded by the user Leigh 18 March 2015, share the link with friends and acquaintances, this video has already been watched 1,013 times on youtube and it was liked by 2 viewers. Enjoy your viewing!