Created
May 11, 2016 09:33
-
-
Save limboinf/312ea1f45d89779e1bb38c6ca6564f2c to your computer and use it in GitHub Desktop.
Python 简单的回显服务器
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
""" | |
echo client | |
:copyright: (c) 2015 by fangpeng. | |
:license: MIT, see LICENSE for more details. | |
""" | |
__date__ = '15/10/24' | |
import socket | |
import sys | |
port = 8050 | |
def echo_client(message): | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect(('localhost', port)) | |
# send datas | |
try: | |
print 'send msg:%s' % message | |
sock.sendall(message) | |
# lock for all respone | |
has_recved = 0 | |
all_datas = len(message) | |
while has_recved < all_datas: | |
recv_data = sock.recv(16) | |
has_recved += len(recv_data) | |
print 'recv data:%s' % recv_data | |
except socket.error, e: | |
print 'socket errro:%s' % e | |
finally: | |
sock.close() | |
if __name__ == '__main__': | |
echo_client(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment