Python Basic Socket network Demo

Publié le: 18 mars 2015
sur la chaîne: 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'))


Sur cette page du site, vous pouvez voir la vidéo en ligne Python Basic Socket network Demo durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Leigh 18 mars 2015, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 1,013 fois et il a aimé 2 téléspectateurs. Bon visionnage!