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
posted @ 2015-08-07 13:15  lily园  Views(252)  Comments(0Edit  收藏  举报