博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python - socket - connection
阅读量:5110 次
发布时间:2019-06-13

本文共 4255 字,大约阅读时间需要 14 分钟。

前面有了TCP server和TCP client。在这个文章中我们建立tcp连接并且进行数据的发送。

例子,经常用到的echo功能。TCP client连接到server, 向server发送message,server端收到消息打印,并将消息返回给client。

server端代码:

import socketimport sysdef 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 socketimport sysdef 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 socketimport sysimport argparsedef 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 socketimport sysimport argparsedef 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

 

转载于:https://www.cnblogs.com/felixwa/p/6044481.html

你可能感兴趣的文章
Jsp抓取页面内容
查看>>
大三上学期软件工程作业之点餐系统(网页版)的一些心得
查看>>
可选参数的函数还可以这样设计!
查看>>
[你必须知道的.NET]第二十一回:认识全面的null
查看>>
Java语言概述
查看>>
关于BOM知识的整理
查看>>
使用word发布博客
查看>>
面向对象的小demo
查看>>
微服务之初了解(一)
查看>>
GDOI DAY1游记
查看>>
收集WebDriver的执行命令和参数信息
查看>>
数据结构与算法(三)-线性表之静态链表
查看>>
mac下的mysql报错:ERROR 1045(28000)和ERROR 2002 (HY000)的解决办法
查看>>
MyBaits动态sql语句
查看>>
HDU4405(期望DP)
查看>>
拉格朗日乘子法 那些年学过的高数
查看>>
vs code 的便捷使用
查看>>
Spring MVC @ResponseBody返回中文字符串乱码问题
查看>>
用户空间与内核空间,进程上下文与中断上下文[总结]
查看>>
JS 中的跨域请求
查看>>