import socket
import sys
import time
import getopt
from threading import thread
class webchat:
#輸出幫助資訊
def usage(self):
print("help info : python webchat.py -h")
print("client : python webchat.py -t [target] -p [port]")
print("server : python webchat.py -lp [port]")
sys.exit()
#聊天客戶端
def webchatclient(self, target, port):
#建立socket套接字
client = socket.socket(socket.af_inet, socket.sock_stream)
#連線伺服器
client.connect((target, port))
print("[*] try to connect the target......")
#接收伺服器發過來的資訊
response = client.recv(1024)
print(response.decode())
#建立傳送訊息的執行緒
t = thread(target=self.senddata, args=(client,))
#指定當前主線程結束時退出子程序
t.setdaemon(true)
#啟動執行緒
t.start()
# 建立接收訊息的執行緒
c = thread(target=self.recvdata, args=("server: ",client,))
c.setdaemon(true)
c.start()
try:
time.sleep(10000)
#keyboardinterrupt用來接收ctrl+c,當接收到命令時就會捕獲,並退出
except keyboardinterrupt:
sys.exit()
#聊天服務端
def webchatserver(self, port):
server = socket.socket(socket.af_inet, socket.sock_stream)
server.bind(("0.0.0.0",port))
server.listen(1)
print("[*] listening on 0.0.0.0:%d"%port)
server_handle, addr = server.accept()
print("[*] accept connection from %s:%d"%(addr[0], addr[1]))
server_handle.send(b"[*] connection successful...")
# 建立傳送訊息的執行緒
t = thread(target=self.senddata, args=(server_handle,))
# 指定當前主線程結束時退出子程序
t.setdaemon(true)
# 啟動執行緒
t.start()
# 建立接收訊息的執行緒
c = thread(target=self.recvdata, args=("client: ", server_handle,))
c.setdaemon(true)
c.start()
try:
time.sleep(10000)
# keyboardinterrupt用來接收ctrl+c,當接收到命令時就會捕獲,並退出
except keyboardinterrupt:
sys.exit()
#傳送資料涵數
def senddata(self, socket):
while true:
data = input()
data = data.encode('utf-8')
socket.send(data)
#接收資料涵數
def recvdata(self,name, socket):
while true:
response = socket.recv(1024)
if response:
response = response.decode()
print(name,response)
if __name__ == "__main__":
target = ""
port = 0
listen = false
help = false
#利用getopt模組獲取從命令列中的引數
opts, args = getopt.getopt(sys.ar**[1:], "t:p:lh")
for o,a in opts:
if o == "-t":
target = a
elif o == "-p":
port = int(a)
elif o == "-l":
listen = true
elif o == "-h":
help == true
else:
assert false, "unhandled option"
test = webchat()
#根據引數區分邏輯
if help:
#輸出幫助資訊
test.usage()
elif listen:
#呼叫伺服器
test.webchatserver(port)
else:
#呼叫客戶端
test.webchatclient(target, port)
即時聊天工具
聊天室客戶端 using system using system.collections.generic using system.componentmodel using system.data using system.drawing using system.linq using system...
web開啟聊天工具
href skype chat 開始 skype 文字聊天a href skype userinfo 檢視 skype 資料a href skype add 新增到 skype 聯絡人列表a href skype call 開始 skype 語音呼叫a href skype voicemail 使用...
聊聊 聊天工具
公司搬家後,對網路的限制越來越嚴格,只允許訪問網頁,主流的聊天工具msn,qq都在禁止之列,雖然一片 聲,卻無濟於事。還好還有google的gtalk,在公司的網路高壓政策下還有一條出路,只是gtalk的朋友比較少,大部分客戶都在qq,msn上,客戶紛紛打 來問怎麼不上網了?真不知如何給客戶解釋公司...