zoukankan      html  css  js  c++  java
  • Python网络编码

    #-*- coding:utf-8 -*-   
    from SocketServer import ThreadingTCPServer, StreamRequestHandler  
    import traceback   
    class MyStreamRequestHandlerr(StreamRequestHandler):  
        def handle(self):  
            while True:  
                try:  
                    data = self.rfile.readline().strip()  
                    print "receive from (%r):%r" % (self.client_address, data)  
                    self.wfile.write(data.upper())  
                except:  
                    traceback.print_exc()  
                    break   
    if __name__ == "__main__":  
        host = ""       #主机名,可以是ip,像localhost的主机名,或""  
        port = 9999     #端口  
        addr = (host, port)  
        server = ThreadingTCPServer(addr, MyStreamRequestHandlerr)  
        server.serve_forever()  
    from socket import *
    import os
    import struct
    ADDR = ('10.1.15.179',8000)
    BUFSIZE = 1024
    filename = '/home/11.txt'
    FILEINFO_SIZE=struct.calcsize('128s32sI8s')
    sendSock = socket(AF_INET,SOCK_STREAM)
    sendSock.connect(ADDR)
    fhead=struct.pack('128s11I',filename,0,0,0,0,0,0,0,0,os.stat(filename).st_size,0,0)
    sendSock.send(fhead)
    fp = open(filename,'rb')
    while 1:
        filedata = fp.read(BUFSIZE)
        if not filedata: break
        sendSock.send(filedata)
    print "ing"
    fp.close()
    sendSock.close()
    print "close.."
    # -*- coding: cp936 -*-
    #code=utf-8
    #from Tkinter import *
    #coding:utf-8
    from socket import *
    import struct
    ADDR = ('',8000)
    BUFSIZE = 1024
    FILEINFO_SIZE=struct.calcsize('128s32sI8s')
    recvSock = socket(AF_INET,SOCK_STREAM)
    recvSock.bind(ADDR)
    recvSock.listen(True)
    print "wait..."
    conn,addr = recvSock.accept()
    print addr
    fhead = conn.recv(FILEINFO_SIZE)
    filename,temp1,filesize,temp2=struct.unpack('128s32sI8s',fhead)
    #print filename,temp1,filesize,temp2
    
    ------------------------------------------------------------------------------------------------------------------------------本娃的学习日记@lily园
  • 相关阅读:
    用场景来规划测试工作
    冲刺第二十天 到二十二天
    冲刺第十九天
    冲刺第十八天
    阅读《构建之法》第13-17章(包含读后感)
    冲刺第5,6天(5月25,26日)
    冲刺第四天(2天合一起当一篇随笔,明天会在这篇里继续更新)
    冲刺第二天
    作业5.2
    作业5.1
  • 原文地址:https://www.cnblogs.com/yanglicyfsdm/p/4710487.html
Copyright © 2011-2022 走看看