程序需要一步步改进,解决bug,尽量从源头判断,并给出处理措施。

1.客户端执行一次,程序就退出,

2.客户端空值,错误命令,程序会死掉

3.收发缓冲区大小,即recv(1024)的问题,如果收一个100M的文件就又是问题

4.多线程问题,同时能处理多个客户端的请求问题

第一次创建了原型

socket单进程服务器

[root@localhost ~]# cat echoserver.py
import socket
host='192.168.10.101'
port=9657
s=socket.socket()
s.bind((host,port))
s.listen(1)
conn,addr=s.accept()
print 'connected by',addr
while 1:
data=conn.recv(20)
if not data:break
conn.sendall(data)
s.close() python echoserver.py fgy@fgy-QTH6:~/Documents/python$ cat echoclient.py
import socket
host='192.168.10.101'
port=9657
s=socket.socket()
s.connect((host,port))
s.sendall('hello,world')
data=s.recv(1024)
print 'received',repr(data)
s.close() python echoclient.py

第二次修改,加入了循环,并持续接收并返回数据

[root@mysql python]# cat echoserver.py
import socket,time,os
host='192.168.10.114'
port=9657
s=socket.socket()
s.bind((host,port))
s.listen(1)
while 1:
conn,addr=s.accept()
while 1:
print 'connected by',addr
print time.ctime()
data=conn.recv(1024)
if not data:break
result=os.popen(data).read()
conn.sendall(result)
s.close() fgy@fgy-QTH6:~/Documents/python$ cat echoclient.py
import socket,time,os
host='192.168.10.114'
port=9657
s=socket.socket()
s.connect((host,port))
while 1:
  cmd=raw_input('your cmd: ').strip()
  s.sendall(cmd)
  data=s.recv(1024)
  print time.ctime()
  print data
#   time.sleep(5)
s.close() os.system(df)不能保存,所以需要用下面的这个
aa=os.popen(df).read()     system(...)
        system(command) -> exit_status
        Execute the command (a string) in a subshell.     popen(...)
        popen(command [, mode='r' [, bufsize]]) -> pipe        
        Open a pipe to/from a command returning a file object. >>> ab=os.popen('free')
>>> ab.read()
'              total        used        free      shared  buff/cache   available\nMem:        3992388     1881176      579816      390708     1531396     1436396\nSwap:       3998716           0     3998716\n'
>>> os.system('free')
              total        used        free      shared  buff/cache   available
Mem:        3992388     1879964      581108      390580     1531316     1437732
Swap:       3998716           0     3998716
0
>>> ac=os.system('free')
              total        used        free      shared  buff/cache   available
Mem:        3992388     1880560      576808      394256     1535020     1433448
Swap:       3998716           0     3998716

第三次,客户端输空值,错误命令的解决办法

针对客户端空值问题

在客户端判断或者在服务器端判断
空值不发过去(在客户端判断),加入下面着色一行就行了
while 1:
      cmd=raw_input('your cmd: ').strip()
     if len(cmd) == 0:continue
      s.sendall(cmd)
      data=s.recv(1024) 错误命令问题分析
在server端,发现下面这个语句不好使
#    result=os.popen(data).read()
所以就换成下面这个模块的一个方法,非常方便简单。
>>> import commands
>>> commands.getstatusoutput('df')
(0, 'Filesystem     1K-blocks     Used Available Use% Mounted on\nudev             1976928        0   1976928   0% /dev\ntmpfs             399240     6432    392808   2% /run\n/dev/sda5       49082176 13552096  33013756  30% /\ntmpfs            1996192    24840   1971352   2% /dev/shm\ntmpfs               5120        4      5116   1% /run/lock\ntmpfs            1996192        0   1996192   0% /sys/fs/cgroup\n/dev/sda6         967320    56100    844868   7% /boot\ntmpfs             399240       80    399160   1% /run/user/1000')
>>> cm=commands.getstatusoutput('df')
>>> cm[0]
0
>>> cm[1]
'Filesystem     1K-blocks     Used Available Use% Mounted on\nudev             1976928        0   1976928   0% /dev\ntmpfs             399240     6432    392808   2% /run\n/dev/sda5       49082176 13552096  33013756  30% /\ntmpfs            1996192    24840   1971352   2% /dev/shm\ntmpfs               5120        4      5116   1% /run/lock\ntmpfs            1996192        0   1996192   0% /sys/fs/cgroup\n/dev/sda6         967320    56100    844868   7% /boot\ntmpfs             399240       80    399160   1% /run/user/1000'
>>> cm=commands.getstatusoutput('dfef')
>>> cm[0]
32512
>>> cm[1]
'sh: 1: dfef: not found' >>> status,result=commands.getstatusoutput('dfef')
>>> status
32512
>>> result
'sh: 1: dfef: not found'
>>> 针对错误命令问题,所以服务端修改为
      data=conn.recv(1024)
      if not data:break
#     result=os.popen(data).read()
     status,result=commands.getstatusoutput(data)
      conn.sendall(result) 针对客户端如果创建文件或目录的话,客户端程序会死掉,所以服务端修改为
加入以下着色部分就可以了
      if not data:break
#     result=os.popen(data).read()
     status,result=commands.getstatusoutput(data)
     if len(result) != 0:
          conn.sendall(result)
     else:
         conn.sendall('DONE') commands.getstatusoutput(data)解决了一些问题,
但是不能执行诸如vmstat 1,top,等有持续输出的命令。
所以还需要考虑另外一些方法

最后成型的程序

fgy@fgy-QTH6:~/Documents/python$ cat echoclient.py
import socket,time,os,commands
host='192.168.10.114'
port=9657
s=socket.socket()
s.connect((host,port))
while 1:
cmd=raw_input('your cmd: ').strip()
if len(cmd) == 0:continue
s.sendall(cmd)
data=s.recv(1024)
print time.ctime()
print data
# time.sleep(5)
s.close() [root@mysql python]# cat echoserver.py
import socket,time,os,commands
host='192.168.10.114'
port=9657
s=socket.socket()
s.bind((host,port))
s.listen(1)
while 1:
conn,addr=s.accept()
while 1:
print 'connected by',addr
print time.ctime()
data=conn.recv(1024)
if not data:break
# result=os.popen(data).read()
status,result=commands.getstatusoutput(data)
if len(result) != 0:
conn.sendall(result)
else:
conn.sendall('done')
s.close()

第四次,循环不等于多线程,与并发也不是一回事,所以需要修改为同时支持多个连接

python有socket模块,还有一个SocketServer多线程模块,有了这个就不需要自己去管理多线程了

RequestHandler.setup()
RequestHandler.handler()
BaseServer.serve_forever(poll-interval=0.5) 重新改写的服务端,还用上面的客户端。但虽然可以允许多个连接过来,但只能交互一次,随后客户端就死掉了,不能循环交互
因为服务端只做了三件事,打印两行,再将数据以大写方式返回给客户端,只能进行一次
所以还需要自己再写循环来实现多次交互。
import SocketServer
class Mytcp(SocketServer.BaseRequestHandler):
def handle(self):
self.data=self.request.recv(1024).strip()
print "{} wrote:".format(self.client_address[0])
print self.data
self.request.sendall(self.data.upper())
if __name__=="__main__":
host,port="192.168.10.114",9657
server=SocketServer.ThreadingTCPServer((host,port),Mytcp)
server.serve_forever() __main__的用途是
当自行调用时,即在命令行主动启动执行的话,才会去执行if下面的内容
如果被当作一个python模块导入的话,不会执行if里的内容。 所以重新修改一下服务端为下面的内容,如果不加if 语句的话,在客户端都退出后,服务器会死循环,搞死服务器。
def handle(self):
    while 1:
        self.data=self.request.recv(1024).strip()
        print "{} wrote:".format(self.client_address[0])
        print self.data
        if not self.data:break
        self.request.sendall(self.data.upper())

第五次修改,文件的传输

下面的客服程序实现了文件下载,与2个并发连接服务器,下载同一个大小为2G的文件,会造成下面的内存报错,并且报错的那条连接下载不成功,如果大小为1G的话,则成功。
原来报错与内存有关系,服务器内存为2G,增加到4G后,3个并发成功。
192.168.10.116 wrote:
get ub
192.168.10.115 wrote:
get ub
----------------------------------------
Exception happened during processing of request from ('192.168.10.115', 55444)
Traceback (most recent call last):
  File "/usr/lib64/python2.7/SocketServer.py", line 593, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib64/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib64/python2.7/SocketServer.py", line 649, in __init__
    self.handle()
  File "sock.py", line 14, in handle
    self.request.sendall(ff.read())
MemoryError 然后需要做的就是上传文件与认证 [root@mysql python]# cat sock.py
import SocketServer,commands,time
class Mytcp(SocketServer.BaseRequestHandler):
def handle(self):
while 1:
self.data=self.request.recv(1024).strip()
print "{} wrote:".format(self.client_address[0])
print self.data
if not self.data:
print "client is dead %s" %self.client_address[0]
break
input=self.data.split()
if input[0] =='get':
with open(input[1],'rb') as ff:
self.request.sendall(ff.read())
time.sleep(2)
self.request.send('file transfer finished')
continue
status,result=commands.getstatusoutput(self.data)
if len(result) != 0:
self.request.sendall(result)
else:
self.request.sendall('done')
#self.request.sendall(self.data.upper()) if __name__=="__main__":
host,port="192.168.10.114",9657
server=SocketServer.ThreadingTCPServer((host,port),Mytcp)
server.serve_forever() fgy@fgy-QTH6:~/Documents/python/ff$ cat echoclient.py
import socket,time,os,commands
host='192.168.10.114'
port=9657
s=socket.socket()
s.connect((host,port))
while 1:
cmd=raw_input('your cmd: ').strip()
if len(cmd) == 0:continue
s.sendall(cmd)
if cmd.split()[0] == 'get':
with open(cmd.split()[1],'wb') as w :
while 1:
data=s.recv(1024)
if data=="file transfer finished":break
w.write(data)
continue
else:
print time.ctime()
print s.recv(1024)
s.close()

python-program的更多相关文章

  1. Release Python Program as exe

    py2exe 可以用来将python program 以exe的形式发布. 安装py2exe 对于python 3.x pip install py2exe 可以直接安装 对于python 2.7, ...

  2. java exec python program

    I find three methods, the first is using jython, the module of jython can transform the type of data ...

  3. My first Python program(附增加清屏方法)

    #TempConvert.py TempStr = input("请输入带有符号的温度值:") if TempStr[-1] in ['F', 'f']: C = (eval(Te ...

  4. Day1 -Python program

    采用python 3.5 用PyCharm编译 第一串代码 print ("hello,world!") 练习1 输入一个用户名和密码,如果输入正确,就欢迎登陆,否则就显示错误. ...

  5. Writing an Hadoop MapReduce Program in Python

    In this tutorial I will describe how to write a simpleMapReduce program for Hadoop in thePython prog ...

  6. 用Python语言写Hadoop MapReduce程序Writing an Hadoop MapReduce Program in Python

    In this tutorial I will describe how to write a simple MapReduce program for Hadoop in the Python pr ...

  7. Python列表和元组

    Python是没有数组的概念,但是和数组比较相近的概念是列表和元素. 下面两个例子展示列表和元组. # coding=utf-8 # 元组 students = ('小明', '小黄', '小李', ...

  8. python与shell的3种交互方式介绍

    [目录] 1.os.system(cmd) 2.os.popen(cmd) 3.利用subprocess模块 4.subprocessor模块进阶 [概述] 考虑这样一个问题,有hello.py脚本, ...

  9. python学习笔记系列----(七)类

    7.1 python类和相关术语的简介 Python 通过最小的新语法和语义在语言中实现了类. 它是 C++ 或者 Modula-3 语言中类机制的混合.类的大多数重要特性都被完整的保留下来:类继承机 ...

  10. 多线程的学习与python实现

    学习了进程与线程,现对自己的学习进行记录. 目录: 一.进程与线程的概念,以及联系与区别 二.多线程 三.python中多线程的应用 四.python实例 五.参考文献 一.进程与线程的概念.以及联系 ...

随机推荐

  1. java语法基本知识

    java中,变量分为局部和成员变量.局部变量在程序运行的过程中在栈stack中分配存储空间. 从上到下是:heap, stack, data segment, code segment.

  2. Consistent hashing —— 一致性哈希

    原文地址:http://www.codeproject.com/Articles/56138/Consistent-hashing 基于BSD License What is libconhash l ...

  3. xml配置文件的读写

    using System.Xml; //----------------------------------------------读出XML文件中的节点值 XmlDocument xmlDoc = ...

  4. 数据导出到excel

    jsp页面: 数据table: <form action="/export.jsp" method="post" id="expform&quo ...

  5. linux下进程突然断掉后的日志查询

    最近写了一个实现监控的服务,可某天去看的时候进程突然木有了,这就捉急了,好好的是啥原因了呢?不会有人不小心把偶的进程关掉了把! 后发现一个linux日志文件:/var/log/messages 打开文 ...

  6. Struts2版本升级到struts2 2.3.15.1操作说明

    Struts2的官网公布了一个远程命令执行漏洞,官方强烈建议升级到2.3.15.1或者以上版本,该版本包含校正过的struts2核心库. 我们之前开发项目主要采用的Struts2版本是2.2.1,本文 ...

  7. C# 字符串操作类

    using System; using System.Collections.Generic; using System.Text; using System.Collections; using S ...

  8. centos7 系统初始化脚本

    现在自己的本地虚拟机系统,直接安装的是centos7.2 mini版,安装完成发现好多东西都没有安装,所以写了一个简单的系统初始化脚本,让自己可以省一些力气,哈哈 人懒主要是. 下面贴出写的脚本,脚本 ...

  9. maya的卡通渲染

    为了统一工作流,给maya也做了个卡通shader:

  10. H3C汇聚层交换机认证在线人数展示系统之需求说明和功能点说明

    一.需求 (一)每五分钟查询一次交换机的连接情况: (二)每2.5分钟更新每栋楼的连接情况. 二.功能点 序号 功能点说明 待定 完成 未完成 完成时间 预计用时(min) 实际用时(min) 备注 ...