Académique Documents
Professionnel Documents
Culture Documents
Servidor
Example: Python Client (TCP)
from socket import *
serverName = ’servername’
create TCP socket for serverPort = 12000
server, remote port 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
sentence = raw_input(‘Input lowercase sentence:’)
No need to attach server
name, port
clientSocket.send(sentence)
modifiedSentence = clientSocket.recv(1024)
print ‘From Server:’, modifiedSentence
clientSocket.close()
Example: Python Server (TCP)
Solution: messages as UDP datagrams
Output Module
def spew(msg, host='localhost', port=PORT):
s = socket.socket((socket.AF_INET,socket.SOCK_DGRAM))
s.bind(('', 0))
while msg:
s.sendto(msg[:BUFSIZE], (host, port))
msg = msg[BUFSIZE:]
An infinite loop, printing out received messages
A Day/Time Client Script
A Day/Time Server Script
A Two‐Way Chat Script
Server creates a socket and enters an infinite loop to
accept/handle clients; when one connects, it sends a
greeting, and enters loop to chat with client
A Two‐Way Chat Script
Client:
- Sets up a socket
- After connection, receives and displays greeting
- Then, enters a loop to chat with server