~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Demo/sockets/echosvr.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
 
 
3
# Python implementation of an 'echo' tcp server: echo all data it receives.
 
4
#
 
5
# This is the simplest possible server, servicing a single request only.
 
6
 
 
7
import sys
 
8
from socket import *
 
9
 
 
10
# The standard echo port isn't very useful, it requires root permissions!
 
11
# ECHO_PORT = 7
 
12
ECHO_PORT = 50000 + 7
 
13
BUFSIZE = 1024
 
14
 
 
15
def main():
 
16
    if len(sys.argv) > 1:
 
17
        port = int(eval(sys.argv[1]))
 
18
    else:
 
19
        port = ECHO_PORT
 
20
    s = socket(AF_INET, SOCK_STREAM)
 
21
    s.bind(('', port))
 
22
    s.listen(1)
 
23
    conn, (remotehost, remoteport) = s.accept()
 
24
    print('connected by', remotehost, remoteport)
 
25
    while 1:
 
26
        data = conn.recv(BUFSIZE)
 
27
        if not data:
 
28
            break
 
29
        conn.send(data)
 
30
 
 
31
main()