小试一下 python写的简单DOS框聊天脚本

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和线程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值