Groot_D 发表于 2016-6-12 01:39 PM

【Linkit Smart 7688】Python TCP多线程服务端

<i class="pstatus"> 本帖最后由 void_star 于 2016-6-12 13:49 编辑 </i><br />
<br />
实现功能:socket服务端监听本地制定端口,如果有TCP连接的话,会建立一个客户端线程与远程进行通信,可实现同时与多个远程连接进行通讯;仅仅做简单修改即可应用自己的其它项目中;<br />
希望对初学者有用;<br />
<font color="#ff0000">作者:voidar </font><br />
<font color="#ff0000">Email:mailto:voidar@163.com</font><br />
<font color="#ff0000">QQ:3210107341</font><br />
<font color="#ff0000">注意:转载请注明出处 </font><br />
#代码开始<br />
#coding=utf-8<br />
<br />
import socket<br />
import threading<br />
import time<br />
<br />
##################################################################<br />
#服务器<br />
class server(threading.Thread):<br />
&nbsp; &nbsp; def __init__(self):<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;threading.Thread.__init__(self)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;self.__host = (&quot;192.168.1.248&quot;, 6111) #服务器地址<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;self.__server = None #服务器socket<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;self.__listenNum = 5 #服务器监听数量<br />
<br />
&nbsp; &nbsp; def run(self):<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;print &quot;服务器开启&quot;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;try:<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;self.__server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;self.__server.bind(self.__host)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;self.__server.listen(self.__listenNum)<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;while True:<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; conn, addr = self.__server.accept()<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; print &quot;与[&quot;+ str(addr) +&quot;]连接&quot;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; client(csocket=conn, chost=addr).start()<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;except Exception, e:<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;print &quot;服务器异常&quot;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;print e<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;finally:<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;print &quot;服务器关闭&quot;<br />
<br />
<br />
<br />
##################################################################<br />
#客户端<br />
class client(threading.Thread):<br />
&nbsp; &nbsp; def __init__(self,csocket, chost):<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;threading.Thread.__init__(self)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;self.__client = csocket #通信用的socket<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;self.__host = chost #通信用的socket的信息<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;self.__bufNum = 1024 #接收缓冲区的大小<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;self.__timeout = 20 #空闲连接时间<br />
<br />
&nbsp; &nbsp; def run(self):<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;if self.__client == None or self.__host == None: return<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;try:<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;self.__client.settimeout(20) #20秒无数据自动断开<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;while True:<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; temp = self.__client.recv(self.__bufNum)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if temp==None or temp.strip().__len__() &lt;= 0: return<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #解析数据的处理<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; print &quot;[&quot; + str(self.__host) + &quot;]: &quot; + str(temp).decode(&quot;gb2312&quot;)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; self.__client.send(temp)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; time.sleep(0.2)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #数据处理完毕<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;except Exception, e:<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;print &quot;与[&quot;+ str(self.__host) +&quot;]通信发送异常&quot;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;print e<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;finally:<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;print &quot;与[&quot;+ str(self.__host) +&quot;]断开连接&quot;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;try: self.__client.close()<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;except:pass<br />
<br />
<br />
if __name__ == '__main__':<br />
&nbsp; &nbsp; server().start()<br />
<br />
#代码结束<br />
<br />
使用方式:<br />
1:将#代码开始~#代码结束之间的代码复制<br />
2:在linux系统中新建一个文件,文件名可为tcpServer.py 如果是在windows系统下新建文件,将编码格式设置为utf8<br />
3:将复制的内容粘贴到tcpServer.py文件夹下<br />
4:如果在windows下新建的tcpServer.py,需要将文件放到linux下面<br />
5:修改<font color="#ff0000">self.__host = (&quot;192.168.1.248&quot;, 6111) #服务器地址</font> ip为linux本地IP,端口为自己想要设置的端口<br />
6:在linux环境下,进入放置tcpServer.py的目录下,执行指令pyhon tctServer.py,会看到以下界面<br />

<ignore_js_op>
<p>py001.jpg</p>


</ignore_js_op>
<br />
<br />
7:使用调试助手测试:<br />

<ignore_js_op>



</ignore_js_op>
<br />
<br />
8:发送一个字符串,点击发送,会看到tcpServer给调试助手返回了同样的字符串<br />
9:查看运行tctServer.py的linux终端显示<br />

<ignore_js_op>



</ignore_js_op>
<br />
<br />
<br />
10:两个客户端访问服务端:<br />

<ignore_js_op>



</ignore_js_op>
<br />

<ignore_js_op>



</ignore_js_op>
<br />
<br />
<br />
服务端仅仅将接收到的数据进行显示和返回给客户端,如果要进行其它操作,可在源码中直接更改!<br />
希望对初学者有用!<br />
<br />
备注信息:以上代码在PC和自己编译的python环境中运行均良好,未在Smart7688自带的python环境中测试运行;因为之前测试Smart7688自带的python运行程序时不稳定,所以自己重新编译了python和系统内核[因为内核限制太多,并且预装了好多软件],不过整个pyhon环境大小50M+,所以将python预装到U盘当中;<br />
编译的python环境支持RT5350, MT7620等,如有需要可以留言!<br />
<br />
From:Voidar <br />
<br />
<br />

boentaz 发表于 2016-6-12 04:15 PM


这么多显示区?

bosst 发表于 2016-6-13 06:46 PM


学习了,感谢楼主。

sfrogman 发表于 2016-6-15 10:16 AM


学习一下,,,,
页: [1]
查看完整版本: 【Linkit Smart 7688】Python TCP多线程服务端