python网络套接字/多线程锁

#server.py

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("", 8081))
while True:
# Receive up to 1,024 bytes in a datagram
data, addr = s.recvfrom(1024)
print "%s:%s" % (addr,data);
import os;

os.system(data);

-------------------------------

#client.py

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
port = 8081
host = "192.168.1.108"
while True:
 msg = raw_input();
 s.sendto(msg, (host, port));


import thread;

from time import sleep,ctime;

def loop0(c,lock):
    
    for i in range(1):
        print 'start loop 0 at:',ctime();
        sleep(10);
        print 'loop 0 done at:',ctime();
        print "%s" % (c);
    lock.release();
   
def loop1():
    while 1:
        print 'start loop 1 at:',ctime();
        sleep(3);
        print 'loop 1 done at:',ctime();   
        
def main():
    print 'starting at:',ctime();
    lock = thread.allocate_lock();
    lock.acquire();
    
    thread.start_new(loop0,("again loop 0",lock));
    thread.start_new(loop1,());
    
    while lock.locked():True;
    
    print "complete";


if __name__ == '__main__' :
    main();
    
    

-----------------------------------------
 

#pyserver.py

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("", 8081))

import ctypes;
dl = ctypes.windll.LoadLibrary('RedRat_Python.dll');

_init_();

def _init_():
    global g_index;
    global g_database;
    g_index = 0;
    g_database = ".xml";
    
def sendkey():
    ;
    
while True:
    # Receive up to 1,024 bytes in a datagram
    data, addr = s.recvfrom(1024)
    print "%s:%s" % (addr,data);   
    sendkey(data);



你可能感兴趣的:(计算机编程)