Python: Echo Server

From OnnoWiki
Revision as of 12:18, 4 May 2014 by Onnowpurbo (talk | contribs) (New page: Here is a simple echo server (echoserver-simple.py): #!/usr/bin/env python """ A simple echo server """ import socket host = '' port = 50000 backlog = 5 size = 1024 s = s...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Here is a simple echo server (echoserver-simple.py):

#!/usr/bin/env python

"""
A simple echo server
"""

import socket 

host = 
port = 50000
backlog = 5
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(backlog)
while 1:
    client, address = s.accept()
    data = client.recv(size)
    if data:
        client.send(data)
    client.close() 


A Simple Echo Client

Here is an echo client that will talk to our echo server (echoclient-simple.py):

#!/usr/bin/env python

"""
A simple echo client
"""

import socket 

host = 'localhost'
port = 50000
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
s.send('Hello, world')
data = s.recv(size)
s.close()
print 'Received:', data  


Referensi