python - socket - connection
前面有了TCP server和TCP client。在这个文章中我们建立tcp连接并且进行数据的发送。
例子,经常用到的echo功能。TCP client连接到server, 向server发送message,server端收到消息打印,并将消息返回给client。
server端代码:
import socket import sys def start_tcp_server(ip, port): #create socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (ip, port) #bind port print 'starting listen on ip %s, port %s'%server_address sock.bind(server_address) #starting listening, allow only one connection try: sock.listen(1) except socket.error, e: print "fail to listen on port %s"%e sys.exit(1) while True: print "waiting for connection" client,addr = sock.accept() print 'having a connection' data = client.recv(1024) print 'recving data:' + data print 'send data back' client.sendall(data) client.close() if __name__ == '__main__': start_tcp_server('172.125.0.1', 12346)
client端代码:
import socket import sys def start_tcp_client(ip, port): #server port and ip server_ip = ip servr_port = port tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: tcp_client.connect((server_ip, servr_port)) except socket.error: print 'fail to setup socket connection' else: print 'sending..........' tcp_client.sendall("echo message") print 'reading...........' print tcp_client.recv(1024) tcp_client.close() start_tcp_client('172.125.0.1', 12346)
这里面用到了收发函数,其中缓存的大小已经指定或者采用默认值。
如何查看一个连接的缓存大小并且在必要的时候修改它。
socket.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, size)
如果是接受方的缓存,参数名称为SO_RCVBUF.
socket.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, size)
我们增加代码加入参数,使用argparser 模块
client:
import socket import sys import argparse def start_tcp_client(ip, port): #server port and ip server_ip = ip servr_port = port tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: tcp_client.connect((server_ip, servr_port)) except socket.error: print 'fail to setup socket connection' else: print 'sending..........' tcp_client.sendall("echo message") print 'reading...........' print tcp_client.recv(1024) tcp_client.close() if __name__ == '__main__': arg = argparse.ArgumentParser(description='input port and ip') arg.add_argument('-i', help='input the ip address') arg.add_argument('-p', type=int, help='input the port') args = arg.parse_args() print 'ip : port ' + args.i +" " + str(args.p) start_tcp_client(args.i, args.p)
server:
import socket import sys import argparse def start_tcp_server(ip, port): #create socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (ip, port) #bind port print 'starting listen on ip %s, port %s'%server_address sock.bind(server_address) #starting listening, allow only one connection try: sock.listen(1) except socket.error, e: print "fail to listen on port %s"%e sys.exit(1) while True: print "waiting for connection" client,addr = sock.accept() print 'having a connection' data = client.recv(1024) print 'recving data:' + data print 'send data back' client.sendall(data) client.close() if __name__ == '__main__': arg = argparse.ArgumentParser(description='input port and ip') arg.add_argument('-i', help='input the ip address') arg.add_argument('-p', type=int, help='input the port') args = arg.parse_args() print 'ip : port ' + args.i +" " + str(args.p) start_tcp_server(args.i, args.p)
调用:
启动server:
# python socket_1_server.py -i 10.135.13.171 -p 12345
ip : port 10.135.13.171 12345
starting listen on ip 10.135.13.171, port 12345
waiting for connection
启动client:
# python socket_2_client.py -i 10.135.13.171 -p 12345
ip : port 10.135.13.171 12345
sending..........
reading...........
echo message
server侧打印:
having a connection
recving data:echo message
send data back
waiting for connectio