import threading
import datetime
import socket
otherip='192.168.3.29'
otherport=8003
yourport=8003
class ServerClass(threading.Thread):
def run(self):
print '1start'
if __name__ == '__main__':
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('0.0.0.0', yourport))
sock.listen(5)
connection,address = sock.accept()
while True:
try:
connection.settimeout(5)
buf = connection.recv(1024)
print ":"+buf
except socket.timeout:
a=1
connection.close()class ClientClass(threading.Thread):
def run(self):
print '2start'
if __name__ == '__main__':
sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
import time
try:
sock1.connect((otherip, otherport))
break
except:
print 'server do not connect'
time.sleep(10)
while True:
import time
time.sleep(2)
message=raw_input('')
sock1.send(message)
print sock1.recv(1024)
sock1.close() t1=ServerClass()
t1.start()
t2=ClientClass()
t2.start()
注意配置说明:
otherip设置你想聊天机器的IP
otherport设置你想聊天机器的端口
yourport 你自己机器开放的监听端口
:表示对方说的话,没冒号表示你自己说的话
这个脚本写得简单,用得只有SOCKET和线程。