Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
muryliang committed Sep 8, 2017
1 parent b26cb19 commit ff28f60
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 9 deletions.
69 changes: 69 additions & 0 deletions virtualenv/mypy3/test/blocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python3
# Foundations of Python Network Programming, Third Edition
# https://github.com/brandom-rhodes/fopnp/blob/mn/py3/chapter05/streamer.py

import socket, struct
from argparse import ArgumentParser
import argparse

header_struct = struct.Struct('!I')

def recvall(sock, length):
blocks = []
while length:
block = sock.recv(length)
if not block:
raise EOFError('socket closed with %d bytes left'
' in this block'.format(length))
length -= len(block)
blocks.append(block)
return b''.join(blocks)

def get_block(sock):
data = recvall(sock, header_struct.size)
(block_length,) = header_struct.unpack(data)
return recvall(sock, block_length)

def put_block(sock, message):
block_length = len(message)
sock.send(header_struct.pack(block_length))
sock.send(message)

def server(address):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(address)
sock.listen(1)
print('Run this script in another window with "-c" to connect')
print('Listening at', sock.getsockname())
sc, sockname = sock.accept()
print('Accept connection from', sockname)
sc.shutdown(socket.SHUT_WR)
while True:
block = get_block(sc)
if not block:
break
print('Block says:', repr(block))
sc.close()
sock.close()

def client(address):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(address)
sock.shutdown(socket.SHUT_RD)
put_block(sock, b'Beautiful is better than ugly.')
put_block(sock, b'Explicit is better than implicit.')
put_block(sock, b'Simple is better than complex.')
put_block(sock, b'')
sock.close()

if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Send and receive over TCP")
parser.add_argument('hostname', nargs='?', default='127.0.0.1',
help='IP address or hostname (default: %(default)s)')
parser.add_argument('-c', action='store_true', help='run as the client')
parser.add_argument('-p', type=int, metavar='port', default=1060, help='TCP port number (default: %(default)s)')
args = parser.parse_args()
function = client if args.c else server
function((args.hostname, args.p))

49 changes: 49 additions & 0 deletions virtualenv/mypy3/test/streamer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python3
# Foundations of Python Network Programming, Third Edition
# https://github.com/brandom-rhodes/fopnp/blob/mn/py3/chapter05/streamer.py

import socket
from argparse import ArgumentParser
import argparse

def server(address):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(address)
sock.listen(1)
print('Run this script in another window with "-c" to connect')
print('listening at {}'.format(sock.getsockname()))
sc, sockname = sock.accept()
print ('Accepted connection from', sockname)
sc.shutdown(socket.SHUT_WR)
message = b''
while True:
more = sc.recv(8192)
if not more:
print('Received zero bytes -end of fle')
break
print('Received {} bytes'.format(len(more)))
message += more
print('message:\n')
print (message.decode('ascii'))
sc.close()
sock.close()

def client(address):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(address)
sock.shutdown(socket.SHUT_RD)
sock.sendall(b'Beautiful is better than ugly.\n')
sock.sendall(b'Explicit is beeter than implicit.\n')
sock.sendall(b'Simple is beeter than complex.\n')
sock.close()

if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Send and receive over TCP")
parser.add_argument('hostname', nargs='?', default='127.0.0.1',
help='IP address or hostname (default: %(default)s)')
parser.add_argument('-c', action='store_true', help='run as the client')
parser.add_argument('-p', type=int, metavar='port', default=1060, help='TCP port number (default: %(default)s)')
args = parser.parse_args()
function = client if args.c else server
function((args.hostname, args.p))
20 changes: 11 additions & 9 deletions virtualenv/mypy3/test/template.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#!/usr/bin/env python3
# Foundations of Python Network Programming, Third Edition
# https://github.com/brandom-rhodes/fopnp/blob/mn/py3/chapter04/www_ping.py
# https://github.com/brandom-rhodes/fopnp/blob/mn/py3/chapter05/streamer.py

import socket
from argparse import ArgumentParser
import argparse

if __name__ == '__main__':
choices = {'client':client, 'server':server}
parser = argparse.ArgumentParser(description="Send and receive over TCP")
parser.add_argument('role', choices = choices, help = 'while role to play')
parser.add_argument('host', help='interface the server listens at;'
'host the client sends to')
parser.add_argument('-p', metavar="PORT", type=int, default=1060,
help='TCP port (default 1060)')
parser.add_argument('hostname', nargs='?', default='127.0.0.1',
help='IP address or hostname (default: %(default)s)')
parser.add_argument('-c', action='store_true', help='run as the client')
parser.add_argument('-p', type=int, metavar='port', default=1060, help='TCP port number (default: %(default)s)')
args = parser.parse_args()
function = choices[args.role]
function(args.host, args.p)
function = client if args.c else server
function((args.hostname, args.p))

0 comments on commit ff28f60

Please sign in to comment.