Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of NSAPITests by
EchoServer.py@4:b52f17273177, 2016-03-02 (annotated)
- Committer:
- Christopher Haster
- Date:
- Wed Mar 02 18:02:47 2016 -0600
- Revision:
- 4:b52f17273177
- Child:
- 11:fbfe3498404a
Changed to just an echo server
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Christopher Haster |
4:b52f17273177 | 1 | #!/usr/bin/env python |
| Christopher Haster |
4:b52f17273177 | 2 | import socket |
| Christopher Haster |
4:b52f17273177 | 3 | import signal |
| Christopher Haster |
4:b52f17273177 | 4 | import select |
| Christopher Haster |
4:b52f17273177 | 5 | |
| Christopher Haster |
4:b52f17273177 | 6 | |
| Christopher Haster |
4:b52f17273177 | 7 | def main(port): |
| Christopher Haster |
4:b52f17273177 | 8 | port = int(port) |
| Christopher Haster |
4:b52f17273177 | 9 | |
| Christopher Haster |
4:b52f17273177 | 10 | udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| Christopher Haster |
4:b52f17273177 | 11 | udp.bind(('', port)) |
| Christopher Haster |
4:b52f17273177 | 12 | udp.setblocking(0) |
| Christopher Haster |
4:b52f17273177 | 13 | udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| Christopher Haster |
4:b52f17273177 | 14 | |
| Christopher Haster |
4:b52f17273177 | 15 | tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| Christopher Haster |
4:b52f17273177 | 16 | tcp.bind(('', port)) |
| Christopher Haster |
4:b52f17273177 | 17 | tcp.setblocking(0) |
| Christopher Haster |
4:b52f17273177 | 18 | tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| Christopher Haster |
4:b52f17273177 | 19 | tcp.listen(5) |
| Christopher Haster |
4:b52f17273177 | 20 | |
| Christopher Haster |
4:b52f17273177 | 21 | print "running on port %d" % port |
| Christopher Haster |
4:b52f17273177 | 22 | sockets = [tcp, udp] |
| Christopher Haster |
4:b52f17273177 | 23 | clients = [] |
| Christopher Haster |
4:b52f17273177 | 24 | |
| Christopher Haster |
4:b52f17273177 | 25 | while True: |
| Christopher Haster |
4:b52f17273177 | 26 | select.select(sockets, [], []) |
| Christopher Haster |
4:b52f17273177 | 27 | |
| Christopher Haster |
4:b52f17273177 | 28 | try: |
| Christopher Haster |
4:b52f17273177 | 29 | data, addr = udp.recvfrom(1 << 12) |
| Christopher Haster |
4:b52f17273177 | 30 | print 'udp %s:%d %d' % (addr[0], addr[1], len(data)) |
| Christopher Haster |
4:b52f17273177 | 31 | udp.sendto(data, addr) |
| Christopher Haster |
4:b52f17273177 | 32 | except socket.error: |
| Christopher Haster |
4:b52f17273177 | 33 | pass |
| Christopher Haster |
4:b52f17273177 | 34 | |
| Christopher Haster |
4:b52f17273177 | 35 | try: |
| Christopher Haster |
4:b52f17273177 | 36 | client, addr = tcp.accept() |
| Christopher Haster |
4:b52f17273177 | 37 | print 'tcp %s:%d connect' % (addr[0], addr[1]) |
| Christopher Haster |
4:b52f17273177 | 38 | client.setblocking(0) |
| Christopher Haster |
4:b52f17273177 | 39 | client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| Christopher Haster |
4:b52f17273177 | 40 | sockets.append(client) |
| Christopher Haster |
4:b52f17273177 | 41 | clients.append((client, addr)) |
| Christopher Haster |
4:b52f17273177 | 42 | except socket.error: |
| Christopher Haster |
4:b52f17273177 | 43 | pass |
| Christopher Haster |
4:b52f17273177 | 44 | |
| Christopher Haster |
4:b52f17273177 | 45 | for client, addr in clients: |
| Christopher Haster |
4:b52f17273177 | 46 | try: |
| Christopher Haster |
4:b52f17273177 | 47 | data = client.recv(1 << 12) |
| Christopher Haster |
4:b52f17273177 | 48 | if data: |
| Christopher Haster |
4:b52f17273177 | 49 | print 'tcp %s:%d %d' % (addr[0], addr[1], len(data)) |
| Christopher Haster |
4:b52f17273177 | 50 | client.send(data) |
| Christopher Haster |
4:b52f17273177 | 51 | else: |
| Christopher Haster |
4:b52f17273177 | 52 | print 'tcp %s:%d disconnect' % (addr[0], addr[1]) |
| Christopher Haster |
4:b52f17273177 | 53 | sockets.remove(client) |
| Christopher Haster |
4:b52f17273177 | 54 | clients.remove((client, addr)) |
| Christopher Haster |
4:b52f17273177 | 55 | client.close() |
| Christopher Haster |
4:b52f17273177 | 56 | except socket.error: |
| Christopher Haster |
4:b52f17273177 | 57 | pass |
| Christopher Haster |
4:b52f17273177 | 58 | |
| Christopher Haster |
4:b52f17273177 | 59 | |
| Christopher Haster |
4:b52f17273177 | 60 | if __name__ == "__main__": |
| Christopher Haster |
4:b52f17273177 | 61 | import sys |
| Christopher Haster |
4:b52f17273177 | 62 | main(*sys.argv[1:]) |
