标签:
研究python服务器开发http://www.example-code.com/python/ssh_exec.asp
telnet 118.193.152.71 51234
import socket, threading HOST = ‘127.0.0.1‘ PORT = 51234 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(4) clients = [] #list of clients connected lock = threading.Lock() class chatServer(threading.Thread): def __init__(self, (socket,address)): threading.Thread.__init__(self) self.socket = socket self.address= address def run(self): lock.acquire() clients.append(self) lock.release() print ‘%s:%s connected.‘ % self.address while True: data = self.socket.recv(1024) if not data: break for c in clients: c.socket.send(data) self.socket.close() print ‘%s:%s disconnected.‘ % self.address lock.acquire() clients.remove(self) lock.release() while True: # wait for socket to connect # send socket to chatserver and start monitoring chatServer(s.accept()).start()
$ START "" .\dns-server.py
$ .\dns-client.py
172.16.45.84
#!/usr/bin/python import socket def resolve(name): if name == "mpapp.nobies.in": return "172.16.45.84" else : # you ought to add some basic checking of name here return socket.gethostbyname(name) host = ‘‘ port = 50000 backlog = 5 size = 1024 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((host,port)) s.listen(backlog) while 1: client, address = s.accept() data = client.recv(size) if data: bits = data.split(":") if bits[0] == ‘h‘: client.send(resolve(bits[1])) client.close()
#!/usr/bin/python import socket ### configure me ### dns_server_ip = ‘127.0.0.1‘ dns_server_port = 50000 query = ‘mpapp.nobies.in‘ # change this to the hostname you want to lookup ### configure me ### size = 1024 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((dns_server_ip,dns_server_port)) s.send(‘h:‘ + query) data = s.recv(size) s.close() print data
标签:
原文地址:http://www.cnblogs.com/franzlistan/p/4598955.html