pythonnotinlisten
1. python怎麼連接websocket
不知道是不是要server和client。
給一個例子,基於py27的聊天室,有server,同時client是用python模擬的,如果你想用js寫,可以自己寫一個
使用前先:
pip install websocket-client
然後運行server、client就可以了
websocket_server.py(服務端)
#-*-encoding:utf-8-*-
importsys
sys.path.append("..")
fromsocketimport*
importjson,time,threading
reload(sys)
sys.setdefaultencoding("utf8")
config={
'HOST':'127.0.0.1',
'PORT':10010,
'LISTEN_CLIENT':50,
'KEY':'',
'SIZE':2048,
'TIME_OUT':10000,
'HEART_TIME':5,
'MAGIC_STRING':'258EAFA5-E914-47DA-95CA-C5AB0DC85B11',
'HANDSHAKE_STRING':"HTTP/1.1101SwitchingProtocols "
"Upgrade:websocket "
"Connection:Upgrade "
"Sec-WebSocket-Accept:{1} "
"WebSocket-Location:ws://{2}/chat "
"WebSocket-Protocol:chat "
}
classServer():
"""
服務端基類
"""
def__init__(self):
self.sock=socket(AF_INET,SOCK_STREAM)
self.sock.bind((config['HOST'],config['PORT']))#監聽埠
self.sock.listen(config['LISTEN_CLIENT'])#監聽客戶端數量
#所有監聽的客戶端
self.clients={}
self.thrs={}
self.users={}
self.stops=[]
self.onlineusers=[]
#監聽客戶端連接
deflisten_client(self):
while1:
#循環監聽
tcpClientSock,addr=self.sock.accept()
address=addr[0]+':'+str(addr[1])#ip:port
#握手
topInfo=tcpClientSock.recv(1024)
headers={}
ifnottopInfo:
self.onlineusers.remove(self.users[address])
tcpClientSock.close()
continue
header,data=topInfo.split(' ',1)
try:
getInfo=header.split(' ')[0].split('')[1].split('/')[1:]
ifgetInfo[0]=='name':
self.users[address]=str(getInfo[1])
self.onlineusers.append(self.users[address])
else:
self.users[address]=u'匿名用戶'
except:
self.users[address]=u'匿名用戶'
print"thisisself.users[address]:"
printself.users[address]
forlineinheader.split(' ')[1:]:
key,val=line.split(':',1)
headers[key]=val
if'Sec-WebSocket-Key'notinheaders:
self.onlineusers.remove(self.users[address])
tcpClientSock.close()
continue
importhashlib,base64
sec_key=headers['Sec-WebSocket-Key']
res_key=base64.b64encode(hashlib.sha1(sec_key+config['MAGIC_STRING']).digest())
str_handshake=config['HANDSHAKE_STRING'].replace('{1}',res_key).replace('{2}',config['HOST']+':'+str(config['PORT']))
tcpClientSock.send(str_handshake)
#握手成功分配線程進行監聽
print(address+u'進來了')
self.clients[address]=tcpClientSock
self.thrs[address]=threading.Thread(target=self.readMsg,args=[address])
self.thrs[address].start()
time.sleep(0.5)
defreadMsg(self,address):
ifaddressnotinself.clients:
returnFalse
client=self.clients[address]
importselect
time_out=0
while1:
ifaddressinself.stops:
self.close_client(address)
print(address+u'已經離開了系統!')
break
#檢測超時
iftime_out>=config['TIME_OUT']:
self.close_client(address)
break
time_out+=5
infds,outfds,errfds=select.select([client,],[],[],5)
iflen(infds)==0:
continue
time_out=0
try:
info=client.recv(1024)
except:
self.close_client(address)
break
ifnotinfo:
continue
ifinfo=='quit':
self.close_client(address)
break
code_len=ord(info[1])&127
ifcode_len==126:
masks=info[4:8]
data=info[8:]
elifcode_len==127:
masks=info[10:14]
data=info[14:]
else:
masks=info[2:6]
data=info[6:]
i=0
raw_str=""
fordindata:
#print(masks,masks[i%4])
raw_str+=chr(ord(d)^ord(masks[i%4]))
i+=1
print"thisisraw_stryesthisisraw_str"
printrepr(raw_str)
#獲取到輸入的數據向所有的客戶端發送
#開啟線程記錄
ifraw_str:
t1=threading.Thread(target=self.send_data,args=[raw_str,address])
t1.start()
time.sleep(0.5)defonlineuser(self):
fromurllibimportunquote
username=[]
foriinself.onlineusers:
i=unquote(i).encode('utf8')
username.append(str(i))
returnusername
defsend_data(self,data,address):
importstruct
fromurllibimportunquote
try:
username=unquote(self.users[address])
except:
username=u'匿名用戶'
'''封裝數據'''
reply={}
ifdata:
data=data.split("}")[0]+"}"
data=json.loads(data);
'''如果有人加入的話,消息封裝如下:'''
ifdata["type"]=="JOIN":
reply["type"]="JOIN"
reply["data"]=data["username"]
reply["user_from"]=data["username"]
reply["user"]=self.onlineuser()
'''如果有人發消息,判斷是發送給全部還是個人'''
elifdata["type"]=="POST":
reply["type"]="POST"
ifdata["sendto"]=="ALL":
reply["data"]="["+data['username']+"]:"+data["content"]
else:
reply["data"]=u"["+data['username']+"]悄悄對["+data["sendto"]+"]說:"+data["content"]
reply["user_from"]=data["username"]
reply["user"]=self.onlineuser()
reply["sendto"]=data["sendto"]
'''如果是退出的消息,封裝如下'''
elifdata["type"]=="QUIT":
self.close_client(address)
reply["type"]="QUIT"
reply["user_from"]=data["username"]
reply["user"]=self.onlineuser()
reply=json.mps(reply)
else:
returnFalse
token="x81"
length=len(reply)
iflength<126:
token+=struct.pack("B",length)
eliflength<=0xFFFF:
token+=struct.pack("!BH",126,length)
else:
token+=struct.pack("!BQ",127,length)
#struct為Python中處理二進制數的模塊,二進制流為C,或網路流的形式。
alldata='%s%s'%(token,reply)
try:
#判斷消息如果是私聊的,找出sendto和sendfrom進行單發
ifdata["type"]=="POST":
ifdata["sendto"]!="ALL":
forkey,valinself.users.iteritems():
printkey,unquote(val)
val=unquote(val)
ifval==data["sendto"]orval==data["username"]:
printkey
self.clients[key].send(alldata)
else:
forkey,valinself.clients.iteritems():
client=val
try:
client.send(alldata)
except:
self.close_client(key)
else:
forkey,valinself.clients.iteritems():
client=val
try:
client.send(alldata)
except:
self.close_client(key)
except:
pass
defclose_client(self,address):
try:
client=self.clients.pop(address)
self.stops.append(address)
self.onlineusers.remove(self.users[address])
client.close()
delself.users[address]
except:
pass
print(address+u'已經退出')if__name__=='__main__':
s=Server()
s.listen_client()websocket_client.py(客戶端)
#-*-encoding:utf-8-*-
importsys
sys.path.append("..")
fromsocketimport*
importjson,time,threading
fromwebsocketimportcreate_connection
reload(sys)
sys.setdefaultencoding("utf8")
#config={
#'HOST':'127.0.0.1',
#'PORT':10010
#}
#pipinstallwebsocket-client
classClient():
def__init__(self):
#調用create_connection方法,建立一個websocket鏈接
self.ws=create_connection("ws://127.0.0.1:10010/name/johanna")
#建一個線程,監聽伺服器發送給客戶端的數據
self.trecv=threading.Thread(target=self.recv)
self.trecv.start()
#建立鏈接後,給聊天室大家群發一個加入消息
msg={
"type":"JOIN",
"username":"johanna",
"content":"HelloEvery,anewguycomein"
}
msg=json.mps(msg)
self.ws.send(msg)
#發送方法,聊天輸入語句時調用,此處默認為群聊ALL
defsend(self,content):
msg={
"type":"POST",
"username":"johanna",
"sendto":"ALL",
"content":content
}
msg=json.mps(msg)
self.ws.send(msg)
#接收服務端發送給客戶的數據,只要ws處於連接狀態,則一直接收數據
defrecv(self):
try:
whileself.ws.connected:
result=self.ws.recv()
print"receivedmsg:"+str(result)
exceptException,e:
pass
#關閉時,發送QUIT方法,並告訴大家,我要走了
defclose(self):
msg={
"type":"QUIT",
"username":"johanna",
"content":"byebye,everyone"
}
msg=json.mps(msg)
self.ws.send(msg)
if__name__=='__main__':
c=Client()
#當輸入聊天語句非exit時,則持續聊天狀態,如果exit,則關閉鏈接
whileTrue:
content=raw_input("pleaseinput(inputexittoexit):")
ifcontent=="exit":
c.close()
break
else:
c.send(content)
time.sleep(1)
2. 小白求教,Python3實例類調用顯示沒定義
問下你的上述類代碼與main代碼都在同一個文件里嗎,如果在不同文件,有沒有import?
3. python3問題
1:
def is_anagram(word1, word2):
return not set(word1) ^ set(word2)
def main():
word1 = input("Enter the first string: ")
word2 = input("Enter the second string: ")
print("listen and silent are %s anagrams." % (
"" if is_anagram(word1, word2) else "not"))
if __name__ == "__main__":
main()
2:
def _list(number_list):
# Write your function code here
return [x[:] for x in number_list[:]]
def main():
number_list = [[1, 2, 3], [4, 5, 6], [8, 9], [10]]
new_list = _list(number_list)
print(number_list)
print(new_list)
number_list.pop()
new_list[0].append([11, 12])
print(number_list)
print(new_list)
4. 如何用Python編寫一個聊天室
聊天窗口效果圖:
classChatFrame(wx.Frame):"""聊天窗口"""def__init__(self,parent,id,title,size):'初始化,添加控制項並綁定事件'wx.Frame.__init__(self,parent,id,title)self.SetSize(size)self.Center()self.chatFrame=wx.TextCtrl(self,pos=(5,5),size=(490,310),style=wx.TE_MULTILINE|wx.TE_READONLY)self.message=wx.TextCtrl(self,pos=(5,320),size=(300,25))self.sendButton=wx.Button(self,label="Send",pos=(310,320),size=(58,25))self.usersButton=wx.Button(self,label="Users",pos=(373,320),size=(58,25))self.closeButton=wx.Button(self,label="Close",pos=(436,320),size=(58,25))self.sendButton.Bind(wx.EVT_BUTTON,self.send)self.usersButton.Bind(wx.EVT_BUTTON,self.lookUsers)self.closeButton.Bind(wx.EVT_BUTTON,self.close)thread.start_new_thread(self.receive,())self.Show()defsend(self,event):'發送消息'message=str(self.message.GetLineText(0)).strip()ifmessage!='':con.write('say'+message+'n')self.message.Clear()deflookUsers(self,event):'查看當前在線用戶'con.write('lookn')defclose(self,event):'關閉窗口'con.write('logoutn')con.close()self.Close()defreceive(self):'接受伺服器的消息'whileTrue:sleep(0.6)result=con.read_very_eager()ifresult!='':self.chatFrame.AppendText(result)
3.客戶端完整代碼
#!/usr/bin/python#encoding:utf-(wx.Frame):"""登錄窗口"""def__init__(self,parent,id,title,size):'初始化,添加控制項並綁定事件'wx.Frame.__init__(self,parent,id,title)self.SetSize(size)self.Center()self.serverAddressLabel=wx.StaticText(self,label="ServerAddress",pos=(10,50),size=(120,25))self.userNameLabel=wx.StaticText(self,label="UserName",pos=(40,100),size=(120,25))self.serverAddress=wx.TextCtrl(self,pos=(120,47),size=(150,25))self.userName=wx.TextCtrl(self,pos=(120,97),size=(150,25))self.loginButton=wx.Button(self,label='Login',pos=(80,145),size=(130,30))self.loginButton.Bind(wx.EVT_BUTTON,self.login)self.Show()deflogin(self,event):'登錄處理'try:serverAddress=self.serverAddress.GetLineText(0).split(':')con.open(serverAddress[0],port=int(serverAddress[1]),timeout=10)response=con.read_some()ifresponse!='ConnectSuccess':self.showDialog('Error','ConnectFail!',(95,20))returncon.write('login'+str(self.userName.GetLineText(0))+'n')response=con.read_some()ifresponse=='UserNameEmpty':self.showDialog('Error','UserNameEmpty!',(135,20))elifresponse=='UserNameExist':self.showDialog('Error','UserNameExist!',(135,20))else:self.Close()ChatFrame(None,-2,title='ShiYanLouChatClient',size=(500,350))exceptException:self.showDialog('Error','ConnectFail!',(95,20))defshowDialog(self,title,content,size):'顯示錯誤信息對話框'dialog=wx.Dialog(self,title=title,size=size)dialog.Center()wx.StaticText(dialog,label=content)dialog.ShowModal()classChatFrame(wx.Frame):"""聊天窗口"""def__init__(self,parent,id,title,size):'初始化,添加控制項並綁定事件'wx.Frame.__init__(self,parent,id,title)self.SetSize(size)self.Center()self.chatFrame=wx.TextCtrl(self,pos=(5,5),size=(490,310),style=wx.TE_MULTILINE|wx.TE_READONLY)self.message=wx.TextCtrl(self,pos=(5,320),size=(300,25))self.sendButton=wx.Button(self,label="Send",pos=(310,320),size=(58,25))self.usersButton=wx.Button(self,label="Users",pos=(373,320),size=(58,25))self.closeButton=wx.Button(self,label="Close",pos=(436,320),size=(58,25))self.sendButton.Bind(wx.EVT_BUTTON,self.send)self.usersButton.Bind(wx.EVT_BUTTON,self.lookUsers)self.closeButton.Bind(wx.EVT_BUTTON,self.close)thread.start_new_thread(self.receive,())self.Show()defsend(self,event):'發送消息'message=str(self.message.GetLineText(0)).strip()ifmessage!='':con.write('say'+message+'n')self.message.Clear()deflookUsers(self,event):'查看當前在線用戶'con.write('lookn')defclose(self,event):'關閉窗口'con.write('logoutn')con.close()self.Close()defreceive(self):'接受伺服器的消息'whileTrue:sleep(0.6)result=con.read_very_eager()ifresult!='':self.chatFrame.AppendText(result)'程序運行'if__name__=='__main__':app=wx.App()con=telnetlib.Telnet()LoginFrame(None,-1,title="Login",size=(280,200))app.MainLoop()
5. python ftp permission denied怎麼解決
FTP 550 Permission denied 只能建文件夾,沒法刪除及上傳文件的解決方法,需要的朋友可以參考下。
出現這種問題,
①首先檢查用戶對應的角色名,然後看路徑設置是否正確,有沒有相關的許可權,如果這些都沒問題,然後就嘗試下面的操作:
②重起FTP服務,最好從服務裡面重啟(不能根本解決);
③重新設置Server-U帳號和目錄訪問里的文件路徑(如果還不行,嘗試下面操作);
④刪除用戶,重新建。
原創 SU整理(FTP)收藏
整理如下:
1開頭-成功
2開頭-成功
3開頭-許可權問題
4開頭-文件問題
5開頭-伺服器問題
150 FILE: %s
150 Opening %s mode data connection for %s (%I64d Bytes).
150 Opening %s mode data connection for %s.
150 Opening ASCII mode data connection for /bin/ls.
150 Opening BINARY mode data connection for MLSD.
200 (end of 'index %s')
200 Command channel switched to clear-text.
200 Command OK, %I64d Bytes available on %s
200 Command okay.
200 EXEC command successful (TID=%u).
200 Message received okay.
200 MLST OPTS
200 MODE S ok.
200 MODE Z LEVEL set to %d.
200 MODE Z ok.
200 Noted.
200 Opened OK (but not really).
200 PBSZ command OK. Protection buffer size set to 0.
200 PBSZ=0
200 PORT Command successful.
200 PROT command OK. Using clear data connection.
200 PROT command OK. Using private data connection.
200 SSCN:CLIENT METHOD
200 SSCN:SERVER METHOD
200 STRU F ok.
200 STRU R ok.
200 Transfer progress notification is off.
200 Transfer progress notification is on.
200 Type set to A.
200 Type set to I.
202 Command not implemented, superfluous at this site.
202 Command not implemented, superfluous at this site.
210 UTC%+ld
211 End
211 End of status.
211-Extension supported
211-Status for user %Name from %IP:
213 %I64d
213 %s
214 Direct comments or bugs to [email protected].
214- The following commands are recognized (* => unimplemented).
215 UNIX Type: L8
220 Service ready for new user.
220 Serv-U FTP Server v%s for WinSock ready...
221 Goodbye!
226 ABOR command successful.
226 Transfer complete.
226 Transfer complete.
226 Transfer complete.
227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)
230 Password changed okay.
230 Switching to SYSTEM MAINTENANCE mode.
230 User logged in, proceed.
230 User logged in.
231 Switching to GROUP MAINTENANCE mode.
232 Switching to DOMAIN MAINTENANCE mode.
233 Switching to READ-ONLY MAINTENANCE mode.
233 User logged in, PASSWORD EXPIRED - please change before proceeding.
234 AUTH command OK. Already have SSL connection.
234 AUTH command OK. Initializing SSL connection.
250 %X
250 DELE command successful.
250 Directory changed to %s
250 End.
250 RMD command successful.
250 RNTO command successful.
250-Listing %s
253 Attributes changed okay.
253 Date/time changed okay.
257 "%s" directory created.
257 "%s" is current directory.
321 Transferred %I64d Bytes.
331 Response to otp-md%d %u %s required for skey.
331 User name okay, need password.
331 User name okay, please send complete E-mail address as password.
350 File or directory exists, ready for destination name
350 Restarting at %I64d. Send STORE or RETRIEVE.
421 Data connection closed (SSL/TLS negotiation failed).
421 Service closing control connection.
425 Cannot open data connection.
425 Cannot open data connection.
425 Cannot open data connection.
425 Try later, data connection in use.
425 Try later, data connection in use.
425 Try later, data connection in use.
425 Try later, out of network sockets.
425 Try later, out of network sockets.
425 Try later, out of network sockets.
425 Unable to listen, too many pending PASV requests from same client IP.
425 Unable to set up passive listening socket.
426 Data connection closed, cannot read file %s.
426 Data connection closed, cannot write file %s - disk full?
426 Data connection closed, error compressing data stream.
426 Data connection closed, error decompressing data stream.
426 Data connection closed, file transfer %s aborted by client.
426 Data connection closed, file transfer %s aborted.
426 Data connection closed, receive file %s aborted.
426 Data connection closed, transfer aborted.
426 Transfer aborted.
431 Account temporarily locked for other user, please try later.
431 Password not changed, no match for old password.
431 Unable to negotiate secure command connection.
432 Password not changed, new password should be at least %d characters in length.
432 Password not changed, new password should contain at least one upper-case character and symbol
432 Password not changed, old and new passwords cannot be identical.
450 Cannot open file %s for reading.
452 Sorry, insufficient disk quota - receive file %s aborted.
500 '%s': command not understood.
501 %s: Not a directory.
501 Cannot EXEC command line (error=%u).
501 Invalid MLST options
501 Invalid option.
501 Not enough space, only %I64d Bytes available on drive %s
501 Permission denied.
501 SITE option not supported.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Syntax error in parameters or arguments.
501 Wildcards not allowed.
502 Syntax error - use SITE PSWD "<oldpassword>" "<newpassword>".
503 Bad sequence of commands.
503 Bad sequence of commands.
503 Secure command connection required prior to PBSZ command.
503 Secure command connection required prior to PROT command.
504 Command not implemented for that parameter.
504 Command not implemented for that parameter.
504 Command not implemented for that parameter.
504 Unknown security mechanism.
510 Cannot determine server time zone.
521 Data connection cannot be opened with this PROT setting.
522 SSL/TLS lib refuses to initiate secure data connection.
530 Cannot change Anonymous password.
530 Cannot change password.
530 Not logged in, cannot find home directory.
530 Not logged in, home directory does not exist.
6. python小白一隻,請問各位大神,執行以下程序時老是出現以下問題
可以根據以下操作步驟執行:
Traceback (most recent call last):
File "C:/Documents and Settings/Administrator/桌面/11", line 10, in <mole>
sock.bind(DEFAULTADDR)
File "<string>", line 1, in bind
error: (10049, "Can't assign requested address")
回溯(最近一次調用):
文件「C:/文件和設置/管理/桌面/11」,10號線,在<模塊>
sock.bind(DEFAULTADDR)
文件「<字元串>」,1號線,在綁定
錯誤:(10049,「無法分配請求的地址」)
7. 如何用python獲取websocket數據
這里,介紹如何使用 Python 與前端 js 進行通信。
websocket 使用 HTTP 協議完成握手之後,不通過 HTTP 直接進行 websocket 通信。
於是,使用 websocket 大致兩個步驟:使用 HTTP 握手,通信。
js 處理 websocket 要使用 ws 模塊; python 處理則使用 socket 模塊建立 TCP 連接即可,比一般的 socket ,只多一個握手以及數據處理的步驟。
握手
過程
包格式
js 客戶端先向伺服器端 python 發送握手包,格式如下:
?
1
2
3
4
5
6
7
8
GET
/chat HTTP/1.1
Host:
server.example.com
Upgrade:
websocket
Connection:
Upgrade
Sec-WebSocket-Key:
dGhlIHNhbXBsZSBub25jZQ==
Origin:
Sec-WebSocket-Protocol:
chat, superchat
Sec-WebSocket-Version:
13
伺服器回應包格式:
?
1
2
3
4
5
HTTP/1.1
101 Switching Protocols
Upgrade:
websocket
Connection:
Upgrade
Sec-WebSocket-Accept:
s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Sec-WebSocket-Protocol:
chat
其中, Sec-WebSocket-Key 是隨機的,伺服器用這些數據構造一個 SHA-1 信息摘要。
方法為: key+migic , SHA-1 加密, base-64 加密,如下:
Python 中的處理代碼
?
1
2
MAGIC_STRING='258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
res_key=base64.b64encode(hashlib.sha1(sec_key
+MAGIC_STRING).digest())
握手完整代碼
js 端
js 中有處理 websocket 的類,初始化後自動發送握手包,如下:
var socket = new WebSocket('ws://localhost:3368');
Python 端
Python 用 socket 接受得到握手字元串,處理後發送
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
HOST='localhost'
PORT=3368
MAGIC_STRING='258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
HANDSHAKE_STRING="HTTP/1.1
101 Switching Protocols\r\n"
\
"Upgrade:websocket\r\n"\
"Connection:
Upgrade\r\n"
\
"Sec-WebSocket-Accept:
{1}\r\n"
\
"WebSocket-Location:ws://{2}/chat\r\n"
\
"WebSocket-Protocol:chat\r\n\r\n"
defhandshake(con):
#con為用socket,accept()得到的socket
headers={}
shake=con.recv(1024)
ifnot
len(shake):
returnFalse
header,
data =shake.split('\r\n\r\n',1)
forline
inheader.split('\r\n')[1:]:
key,
val =line.split(':
',1)
headers[key]=val
if'Sec-WebSocket-Key'
not
in
headers:
print('This
socket is not websocket, client close.')
con.close()
returnFalse
sec_key=headers['Sec-WebSocket-Key']
res_key=base64.b64encode(hashlib.sha1(sec_key
+MAGIC_STRING).digest())
str_handshake=HANDSHAKE_STRING.replace('{1}',
res_key).replace('{2}',
HOST +':'
+
str(PORT))
printstr_handshake
con.send(str_handshake)
returnTrue
通信
不同版本的瀏覽器定義的數據幀格式不同, Python 發送和接收時都要處理得到符合格式的數據包,才能通信。
Python 接收
Python 接收到瀏覽器發來的數據,要解析後才能得到其中的有用數據。
瀏覽器包格式
固定位元組:
( 1000 0001 或是 1000 0002 )這里沒用,忽略
包長度位元組:
第一位肯定是 1 ,忽略。剩下 7 個位可以得到一個整數 (0 ~ 127) ,其中
( 1-125 )表此位元組為長度位元組,大小即為長度;
(126)表接下來的兩個位元組才是長度;
(127)表接下來的八個位元組才是長度;
用這種變長的方式表示數據長度,節省數據位。
mark 掩碼:
mark 掩碼為包長之後的 4 個位元組,之後的兄弟數據要與 mark 掩碼做運算才能得到真實的數據。
兄弟數據:
得到真實數據的方法:將兄弟數據的每一位 x ,和掩碼的第 i%4 位做 xor 運算,其中 i 是 x 在兄弟數據中的索引。
完整代碼
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
defrecv_data(self,
num):
try:
all_data=self.con.recv(num)
ifnot
len(all_data):
returnFalse
except:
returnFalse
else:
code_len=ord(all_data[1])
& 127
ifcode_len
==126:
masks=all_data[4:8]
data=all_data[8:]
elifcode_len
==127:
masks=all_data[10:14]
data=all_data[14:]
else:
masks=all_data[2:6]
data=all_data[6:]
raw_str=""
i=0
ford
indata:
raw_str+=chr(ord(d)
^ ord(masks[i%4]))
i+=1
returnraw_str
js 端的 ws 對象,通過 ws.send(str) 即可發送
ws.send(str)
Python 發送
Python 要包數據發送,也需要處理,發送包格式如下
固定位元組:固定的 1000 0001( 『 \x81 ′ )
包長:根據發送數據長度是否超過 125 , 0xFFFF(65535) 來生成 1 個或 3 個或 9 個位元組,來代表數據長度。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
defsend_data(self,
data):
ifdata:
data=str(data)
else:
returnFalse
token="\x81"
length=len(data)
iflength
< 126:
token+=struct.pack("B",
length)
eliflength
<=0xFFFF:
token+=struct.pack("!BH",126,
length)
else:
token+=struct.pack("!BQ",127,
length)
#struct為Python中處理二進制數的模塊,二進制流為C,或網路流的形式。
data='%s%s'
%
(token, data)
self.con.send(data)
returnTrue
js 端通過回調函數 ws.onmessage() 接受數據
?
1
2
3
4
5
ws.onmessage
= function(result,nTime){
alert("從服務端收到的數據:");
alert("最近一次發送數據到現在接收一共使用時間:"+
nTime);
console.log(result);
}
最終代碼
Python服務端
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#
_*_ coding:utf-8 _*_
__author__='Patrick'
importsocket
importthreading
importsys
importos
importMySQLdb
importbase64
importhashlib
importstruct
#
====== config ======
HOST='localhost'
PORT=3368
MAGIC_STRING='258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
HANDSHAKE_STRING="HTTP/1.1
101 Switching Protocols\r\n"
\
"Upgrade:websocket\r\n"\
"Connection:
Upgrade\r\n"
\
"Sec-WebSocket-Accept:
{1}\r\n"
\
"WebSocket-Location:ws://{2}/chat\r\n"
\
"WebSocket-Protocol:chat\r\n\r\n"
classTh(threading.Thread):
def__init__(self,
connection,):
threading.Thread.__init__(self)
self.con=connection
defrun(self):
whileTrue:
try:
pass
self.con.close()
defrecv_data(self,
num):
try:
all_data=self.con.recv(num)
ifnot
len(all_data):
returnFalse
except:
returnFalse
else:
code_len=ord(all_data[1])
& 127
ifcode_len
==126:
masks=all_data[4:8]
data=all_data[8:]
elifcode_len
==127:
masks=all_data[10:14]
data=all_data[14:]
else:
masks=all_data[2:6]
data=all_data[6:]
raw_str=""
i=0
ford
indata:
raw_str+=chr(ord(d)
^ ord(masks[i%4]))
i+=1
returnraw_str
#
send data
defsend_data(self,
data):
ifdata:
data=str(data)
else:
returnFalse
token="\x81"
length=len(data)
iflength
< 126:
token+=struct.pack("B",
length)
eliflength
<=0xFFFF:
token+=struct.pack("!BH",126,
length)
else:
token+=struct.pack("!BQ",127,
length)
#struct為Python中處理二進制數的模塊,二進制流為C,或網路流的形式。
data='%s%s'
%
(token, data)
self.con.send(data)
returnTrue
#
handshake
defhandshake(con):
headers={}
shake=con.recv(1024)
ifnot
len(shake):
returnFalse
header,
data =shake.split('\r\n\r\n',1)
forline
inheader.split('\r\n')[1:]:
key,
val =line.split(':
',1)
headers[key]=val
if'Sec-WebSocket-Key'
not
in
headers:
print('This
socket is not websocket, client close.')
con.close()
returnFalse
sec_key=headers['Sec-WebSocket-Key']
res_key=base64.b64encode(hashlib.sha1(sec_key
+MAGIC_STRING).digest())
str_handshake=HANDSHAKE_STRING.replace('{1}',
res_key).replace('{2}',
HOST +':'
+
str(PORT))
printstr_handshake
con.send(str_handshake)
returnTrue
defnew_service():
"""start
a service socket and listen
when
coms a connection, start a new thread to handle it"""
sock=socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
try:
sock.bind(('localhost',3368))
sock.listen(1000)
#鏈接隊列大小
print"bind
3368,ready to use"
except:
print("Server
is already running,quit")
sys.exit()
whileTrue:
connection,
address =sock.accept()
#返回元組(socket,add),accept調用時會進入waite狀態
print"Got
connection from ",
address
ifhandshake(connection):
print"handshake
success"
try:
t=Th(connection,
layout)
t.start()
print'new
thread for client ...'
except:
print'start
new thread error'
connection.close()
if__name__
=='__main__':
new_service()
js客戶 端
?
1
2
3
4
5
6
7
8
<script>
varsocket
= newWebSocket('ws://localhost:3368');
ws.onmessage
= function(result,nTime){
alert("從服務端收到的數據:");
alert("最近一次發送數據到現在接收一共使用時間:"+
nTime);
console.log(result);
}
</script>
8. 如何用Python編寫一個聊天室
python聊天室(python2.7版本):
暫時先給出兩種版本的,tcp+udp
都是分別運行server.py和client.py,就可以進行通訊了。
別外還有websocket版本,這個是有web界面的和基本web服務的,如果需要的話,我會把基本的代碼貼一版上來。
TCP版本:
socket-tcp-server.py(服務端):
#-*-encoding:utf-8-*-
#socket.getaddrinfo(host,port,family=0,socktype=0,proto=0,flags=0)
#根據給定的參數host/port,相應的轉換成一個包含用於創建socket對象的五元組,
#參數host為域名,以字元串形式給出代表一個IPV4/IPV6地址或者None.
#參數port如果字元串形式就代表一個服務名,比如「http」"ftp""email"等,或者為數字,或者為None
#參數family為地主族,可以為AF_INET,AF_INET6,AF_UNIX.
#參數socktype可以為SOCK_STREAM(TCP)或者SOCK_DGRAM(UDP)
#參數proto通常為0可以直接忽略
#參數flags為AI_*的組合,比如AI_NUMERICHOST,它會影響函數的返回值
#附註:給參數host,port傳遞None時建立在C基礎,通過傳遞NULL。
#該函數返回一個五元組(family,socktype,proto,canonname,sockaddr),同時第五個參數sockaddr也是一個二元組(address,port)
#更多的方法及鏈接請訪問
#Echoserverprogram
fromsocketimport*
importsys
importthreading
fromtimeimportctime
fromtimeimportlocaltime
importtraceback
importtime
importsubprocess
reload(sys)
sys.setdefaultencoding("utf8")
HOST='127.0.0.1'
PORT=8555#設置偵聽埠
BUFSIZ=1024
classTcpServer():
def__init__(self):
self.ADDR=(HOST,PORT)
try:
self.sock=socket(AF_INET,SOCK_STREAM)
print'%disopen'%PORT
self.sock.bind(self.ADDR)
self.sock.listen(5)
#設置退出條件
self.STOP_CHAT=False
#所有監聽的客戶端
self.clients={}
self.thrs={}
self.stops=[]
exceptException,e:
print"%disdown"%PORT
returnFalse
defIsOpen(ip,port):
s=socket(AF_INET,SOCK_STREAM)
try:
s.connect((ip,int(port)))
#s.shutdown(2)
#利用shutdown()函數使socket雙向數據傳輸變為單向數據傳輸。shutdown()需要一個單獨的參數,
#該參數表示s了如何關閉socket。具體為:0表示禁止將來讀;1表示禁止將來寫;2表示禁止將來讀和寫。
print'%disopen'%port
returnTrue
except:
print'%disdown'%port
returnFalse
deflisten_client(self):
whilenotself.STOP_CHAT:
print(u'等待接入,偵聽埠:%d'%(PORT))
self.tcpClientSock,self.addr=self.sock.accept()
print(u'接受連接,客戶端地址:',self.addr)
address=self.addr
#將建立的clientsocket鏈接放到列表self.clients中
self.clients[address]=self.tcpClientSock
#分別將每個建立的鏈接放入進程中,接收且分發消息
self.thrs[address]=threading.Thread(target=self.readmsg,args=[address])
self.thrs[address].start()
time.sleep(0.5)defreadmsg(self,address):
#如果地址不存在,則返回False
ifaddressnotinself.clients:
returnFalse
#得到發送消息的clientsocket
client=self.clients[address]
whileTrue:
try:
#獲取到消息內容data
data=client.recv(BUFSIZ)
except:
print(e)
self.close_client(address)
break
ifnotdata:
break
#python3使用bytes,所以要進行編碼
#s='%s發送給我的信息是:[%s]%s'%(addr[0],ctime(),data.decode('utf8'))
#對日期進行一下格式化
ISOTIMEFORMAT='%Y-%m-%d%X'
stime=time.strftime(ISOTIMEFORMAT,localtime())
s=u'%s發送給我的信息是:%s'%(str(address),data.decode('utf8'))
#將獲得的消息分發給鏈接中的clientsocket
forkinself.clients:
self.clients[k].send(s.encode('utf8'))
self.clients[k].sendall('sendall:'+s.encode('utf8'))
printstr(k)
print([stime],':',data.decode('utf8'))
#如果輸入quit(忽略大小寫),則程序退出
STOP_CHAT=(data.decode('utf8').upper()=="QUIT")
ifSTOP_CHAT:
print"quit"
self.close_client(address)
print"alreadyquit"
break
defclose_client(self,address):
try:
client=self.clients.pop(address)
self.stops.append(address)
client.close()
forkinself.clients:
self.clients[k].send(str(address)+u"已經離開了")
except:
pass
print(str(address)+u'已經退出')
if__name__=='__main__':
tserver=TcpServer()
tserver.listen_client()
——————————華麗的分割線——————————
socket-tcp-client.py(客戶端):
#-*-encoding:utf-8-*-
fromsocketimport*
importsys
importthreading
importtime
reload(sys)
sys.setdefaultencoding("utf8")
#測試,連接本機
HOST='127.0.0.1'
#設置偵聽埠
PORT=8555
BUFSIZ=1024
classTcpClient:
ADDR=(HOST,PORT)
def__init__(self):
self.HOST=HOST
self.PORT=PORT
self.BUFSIZ=BUFSIZ
#創建socket連接
self.client=socket(AF_INET,SOCK_STREAM)
self.client.connect(self.ADDR)
#起一個線程,監聽接收的信息
self.trecv=threading.Thread(target=self.recvmsg)
self.trecv.start()
defsendmsg(self):
#循環發送聊天消息,如果socket連接存在則一直循環,發送quit時關閉鏈接
whileself.client.connect_ex(self.ADDR):
data=raw_input('>:')
ifnotdata:
break
self.client.send(data.encode('utf8'))
print(u'發送信息到%s:%s'%(self.HOST,data))
ifdata.upper()=="QUIT":
self.client.close()
printu"已關閉"
break
defrecvmsg(self):
#接收消息,如果鏈接一直存在,則持續監聽接收消息
try:
whileself.client.connect_ex(self.ADDR):
data=self.client.recv(self.BUFSIZ)
print(u'從%s收到信息:%s'%(self.HOST,data.decode('utf8')))
exceptException,e:
printstr(e)
if__name__=='__main__':
client=TcpClient()
client.sendmsg()
UDP版本:
socket-udp-server.py
#-*-coding:utf8-*-
importsys
importtime
importtraceback
importthreading
reload(sys)
sys.setdefaultencoding('utf-8')
importsocket
importtraceback
HOST="127.0.0.1"
PORT=9555
CHECK_PERIOD=20
CHECK_TIMEOUT=15
classUdpServer(object):
def__init__(self):
self.clients=[]
self.beats={}
self.ADDR=(HOST,PORT)
try:
self.sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
self.sock.bind(self.ADDR)#綁定同一個域名下的所有機器
self.beattrs=threading.Thread(target=self.checkheartbeat)
self.beattrs.start()
exceptException,e:
traceback.print_exc()
returnFalse
deflisten_client(self):
whileTrue:
time.sleep(0.5)
print"hohohohohoo"
try:
recvData,address=self.sock.recvfrom(2048)
ifnotrecvData:
self.close_client(address)
break
ifaddressinself.clients:
senddata=u"%s發送給我的信息是:%s"%(str(address),recvData.decode('utf8'))
ifrecvData.upper()=="QUIT":
self.close_client(address)
ifrecvData=="HEARTBEAT":
self.heartbeat(address)
continue
else:
self.clients.append(address)
senddata=u"%s發送給我的信息是:%s"%(str(address),u'進入了聊天室')
forcinself.clients:
try:
self.sock.sendto(senddata,c)
exceptException,e:
printstr(e)
self.close_client(c)
exceptException,e:
#traceback.print_exc()
printstr(e)
pass
defheartbeat(self,address):
self.beats[address]=time.time()
defcheckheartbeat(self):
whileTrue:
print"checkheartbeat"
printself.beats
try:
forcinself.clients:
printtime.time()
printself.beats[c]
ifself.beats[c]+CHECK_TIMEOUT<time.time():
printu"%s心跳超時,連接已經斷開"%str(c)
self.close_client(c)
else:
printu"checkp%s,沒有斷開"%str(c)
exceptException,e:
traceback.print_exc()
printstr(e)
pass
time.sleep(CHECK_PERIOD)
defclose_client(self,address):
try:
ifaddressinself.clients:
self.clients.remove(address)
ifself.beats.has_key(address):
delself.beats[address]
printself.clients
forcinself.clients:
self.sock.sendto(u'%s已經離開了'%str(address),c)
print(str(address)+u'已經退出')
exceptException,e:
printstr(e)
raise
if__name__=="__main__":
udpServer=UdpServer()
udpServer.listen_client()
——————————華麗的分割線——————————
socket-udp-client.py:
#-*-coding:utf8-*-
importsys
importthreading
importtime
reload(sys)
sys.setdefaultencoding('utf-8')
importsocket
HOST="127.0.0.1"
PORT=9555
#BEAT_PORT=43278
BEAT_PERIOD=5
classUdpClient(object):
def__init__(self):
self.clientsock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
self.HOST=HOST
self.ADDR=(HOST,PORT)
self.clientsock.sendto(u'請求建立鏈接',self.ADDR)
self.recvtrs=threading.Thread(target=self.recvmsg)
self.recvtrs.start()
self.hearttrs=threading.Thread(target=self.heartbeat)
self.hearttrs.start()
defsendmsg(self):
whileTrue:
data=raw_input(">:")
ifnotdata:
break
self.clientsock.sendto(data.encode('utf-8'),self.ADDR)
ifdata.upper()=='QUIT':
self.clientsock.close()
break
defheartbeat(self):
whileTrue:
self.clientsock.sendto('HEARTBEAT',self.ADDR)
time.sleep(BEAT_PERIOD)
defrecvmsg(self):
whileTrue:
recvData,addr=self.clientsock.recvfrom(1024)
ifnotrecvData:
break
print(u'從%s收到信息:%s'%(self.HOST,recvData.decode('utf8')))if__name__=="__main__":
udpClient=UdpClient()
udpClient.sendmsg()
9. 兩個python問題
#1.t.Usethefollowingfunctionheader:
#defeliminate_plicates(my_list):
#Write
#,
#,callsthefunction
#(eliminate_plicates)
#thefunction.
#:
defeliminate_plicates(my_list):
#Writethefunctioncodehere
rtLst=[]
foriteminmy_list:
ifitemnotinrtLst:
rtLst.append(item)
returnrtLst
defmain():
#Writethemainfunction
strNum=raw_input("")
strNumSplit=strNum.split('')
iNumSplit=[]
foriteminstrNumSplit:
iNumSplit.append(ord(item)-ord('0'))
printeliminate_plicates(iNumSplit)
#Callthemainfunction
main()
#2.Write
#
#iftheyareandFalsetheyarenot.Twowordsareanagramsifthey
#containthesameletters.Forexample,"silent"and"listen"are
#anagrams.Usethefollowingfunctionheader:
#jdefis_anagram(word1,word2):
#Writeamainfunction
#,callstheis_anagram
#.
defis_anagram(word1,word2):
#Writethefunctioncodehere
iflen(word1)!=len(word2):
returnFalse
lstFirst=[]
forcharinword1:
lstFirst.append(char)
lstSecond=[]
forcharinword2:
lstSecond.append(char)
foritem1inlstFirst:
iflstSecond.count(item1)!=1:
returnFalse
returnTrue
defmain():
#Writethemainfunction
strFirst=raw_input("TheFirst: ")
strSecond=raw_input("TheSecond: ")
printis_anagram(strFirst,strSecond)
#Callthemainfunction
main()
10. python怎麼建立socket服務端
socket伺服器再細分可分為多種了,tcp,udp,websocket,都是調用socket模塊,但是具體實現起來有一點細微的差別
先給出一個tcp和udp通過socket協議實現的聊天室的例子
python聊天室(python2.7版本):
都是分別運行server.py和client.py,就可以進行通訊了。
TCP版本:
socket-tcp-server.py(服務端):
#-*-encoding:utf-8-*-
#socket.getaddrinfo(host,port,family=0,socktype=0,proto=0,flags=0)
#根據給定的參數host/port,相應的轉換成一個包含用於創建socket對象的五元組,
#參數host為域名,以字元串形式給出代表一個IPV4/IPV6地址或者None.
#參數port如果字元串形式就代表一個服務名,比如「http」"ftp""email"等,或者為數字,或者為None
#參數family為地主族,可以為AF_INET,AF_INET6,AF_UNIX.
#參數socktype可以為SOCK_STREAM(TCP)或者SOCK_DGRAM(UDP)
#參數proto通常為0可以直接忽略
#參數flags為AI_*的組合,比如AI_NUMERICHOST,它會影響函數的返回值
#附註:給參數host,port傳遞None時建立在C基礎,通過傳遞NULL。
#該函數返回一個五元組(family,socktype,proto,canonname,sockaddr),同時第五個參數sockaddr也是一個二元組(address,port)
#更多的方法及鏈接請訪問
#Echoserverprogram
fromsocketimport*
importsys
importthreading
fromtimeimportctime
fromtimeimportlocaltime
importtraceback
importtime
importsubprocess
reload(sys)
sys.setdefaultencoding("utf8")
HOST='127.0.0.1'
PORT=8555#設置偵聽埠
BUFSIZ=1024
classTcpServer():
def__init__(self):
self.ADDR=(HOST,PORT)
try:
self.sock=socket(AF_INET,SOCK_STREAM)
print'%disopen'%PORT
self.sock.bind(self.ADDR)
self.sock.listen(5)
#設置退出條件
self.STOP_CHAT=False
#所有監聽的客戶端
self.clients={}
self.thrs={}
self.stops=[]
exceptException,e:
print"%disdown"%PORT
returnFalse
defIsOpen(ip,port):
s=socket(AF_INET,SOCK_STREAM)
try:
s.connect((ip,int(port)))
#s.shutdown(2)
#利用shutdown()函數使socket雙向數據傳輸變為單向數據傳輸。shutdown()需要一個單獨的參數,
#該參數表示s了如何關閉socket。具體為:0表示禁止將來讀;1表示禁止將來寫;2表示禁止將來讀和寫。
print'%disopen'%port
returnTrue
except:
print'%disdown'%port
returnFalse
deflisten_client(self):
whilenotself.STOP_CHAT:
print(u'等待接入,偵聽埠:%d'%(PORT))
self.tcpClientSock,self.addr=self.sock.accept()
print(u'接受連接,客戶端地址:',self.addr)
address=self.addr
#將建立的clientsocket鏈接放到列表self.clients中
self.clients[address]=self.tcpClientSock
#分別將每個建立的鏈接放入進程中,接收且分發消息
self.thrs[address]=threading.Thread(target=self.readmsg,args=[address])
self.thrs[address].start()
time.sleep(0.5)defreadmsg(self,address):
#如果地址不存在,則返回False
ifaddressnotinself.clients:
returnFalse
#得到發送消息的clientsocket
client=self.clients[address]
whileTrue:
try:
#獲取到消息內容data
data=client.recv(BUFSIZ)
except:
print(e)
self.close_client(address)
break
ifnotdata:
break
#python3使用bytes,所以要進行編碼
#s='%s發送給我的信息是:[%s]%s'%(addr[0],ctime(),data.decode('utf8'))
#對日期進行一下格式化
ISOTIMEFORMAT='%Y-%m-%d%X'
stime=time.strftime(ISOTIMEFORMAT,localtime())
s=u'%s發送給我的信息是:%s'%(str(address),data.decode('utf8'))
#將獲得的消息分發給鏈接中的clientsocket
forkinself.clients:
self.clients[k].send(s.encode('utf8'))
self.clients[k].sendall('sendall:'+s.encode('utf8'))
printstr(k)
print([stime],':',data.decode('utf8'))
#如果輸入quit(忽略大小寫),則程序退出
STOP_CHAT=(data.decode('utf8').upper()=="QUIT")
ifSTOP_CHAT:
print"quit"
self.close_client(address)
print"alreadyquit"
break
defclose_client(self,address):
try:
client=self.clients.pop(address)
self.stops.append(address)
client.close()
forkinself.clients:
self.clients[k].send(str(address)+u"已經離開了")
except:
pass
print(str(address)+u'已經退出')
if__name__=='__main__':
tserver=TcpServer()
tserver.listen_client()
——————————華麗的分割線——————————
socket-tcp-client.py(客戶端):
#-*-encoding:utf-8-*-
fromsocketimport*
importsys
importthreading
importtime
reload(sys)
sys.setdefaultencoding("utf8")
#測試,連接本機
HOST='127.0.0.1'
#設置偵聽埠
PORT=8555
BUFSIZ=1024
classTcpClient:
ADDR=(HOST,PORT)
def__init__(self):
self.HOST=HOST
self.PORT=PORT
self.BUFSIZ=BUFSIZ
#創建socket連接
self.client=socket(AF_INET,SOCK_STREAM)
self.client.connect(self.ADDR)
#起一個線程,監聽接收的信息
self.trecv=threading.Thread(target=self.recvmsg)
self.trecv.start()
defsendmsg(self):
#循環發送聊天消息,如果socket連接存在則一直循環,發送quit時關閉鏈接
whileself.client.connect_ex(self.ADDR):
data=raw_input('>:')
ifnotdata:
break
self.client.send(data.encode('utf8'))
print(u'發送信息到%s:%s'%(self.HOST,data))
ifdata.upper()=="QUIT":
self.client.close()
printu"已關閉"
break
defrecvmsg(self):
#接收消息,如果鏈接一直存在,則持續監聽接收消息
try:
whileself.client.connect_ex(self.ADDR):
data=self.client.recv(self.BUFSIZ)
print(u'從%s收到信息:%s'%(self.HOST,data.decode('utf8')))
exceptException,e:
printstr(e)
if__name__=='__main__':
client=TcpClient()
client.sendmsg()
UDP版本:
socket-udp-server.py
#-*-coding:utf8-*-
importsys
importtime
importtraceback
importthreading
reload(sys)
sys.setdefaultencoding('utf-8')
importsocket
importtraceback
HOST="127.0.0.1"
PORT=9555
CHECK_PERIOD=20
CHECK_TIMEOUT=15
classUdpServer(object):
def__init__(self):
self.clients=[]
self.beats={}
self.ADDR=(HOST,PORT)
try:
self.sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
self.sock.bind(self.ADDR)#綁定同一個域名下的所有機器
self.beattrs=threading.Thread(target=self.checkheartbeat)
self.beattrs.start()
exceptException,e:
traceback.print_exc()
returnFalse
deflisten_client(self):
whileTrue:
time.sleep(0.5)
print"hohohohohoo"
try:
recvData,address=self.sock.recvfrom(2048)
ifnotrecvData:
self.close_client(address)
break
ifaddressinself.clients:
senddata=u"%s發送給我的信息是:%s"%(str(address),recvData.decode('utf8'))
ifrecvData.upper()=="QUIT":
self.close_client(address)
ifrecvData=="HEARTBEAT":
self.heartbeat(address)
continue
else:
self.clients.append(address)
senddata=u"%s發送給我的信息是:%s"%(str(address),u'進入了聊天室')
forcinself.clients:
try:
self.sock.sendto(senddata,c)
exceptException,e:
printstr(e)
self.close_client(c)
exceptException,e:
#traceback.print_exc()
printstr(e)
pass
defheartbeat(self,address):
self.beats[address]=time.time()
defcheckheartbeat(self):
whileTrue:
print"checkheartbeat"
printself.beats
try:
forcinself.clients:
printtime.time()
printself.beats[c]
ifself.beats[c]+CHECK_TIMEOUT<time.time():
printu"%s心跳超時,連接已經斷開"%str(c)
self.close_client(c)
else:
printu"checkp%s,沒有斷開"%str(c)
exceptException,e:
traceback.print_exc()
printstr(e)
pass
time.sleep(CHECK_PERIOD)
defclose_client(self,address):
try:
ifaddressinself.clients:
self.clients.remove(address)
ifself.beats.has_key(address):
delself.beats[address]
printself.clients
forcinself.clients:
self.sock.sendto(u'%s已經離開了'%str(address),c)
print(str(address)+u'已經退出')
exceptException,e:
printstr(e)
raise
if__name__=="__main__":
udpServer=UdpServer()
udpServer.listen_client()
——————————華麗的分割線——————————
socket-udp-client.py:
#-*-coding:utf8-*-
importsys
importthreading
importtime
reload(sys)
sys.setdefaultencoding('utf-8')
importsocket
HOST="127.0.0.1"
PORT=9555
#BEAT_PORT=43278
BEAT_PERIOD=5
classUdpClient(object):
def__init__(self):
self.clientsock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
self.HOST=HOST
self.ADDR=(HOST,PORT)
self.clientsock.sendto(u'請求建立鏈接',self.ADDR)
self.recvtrs=threading.Thread(target=self.recvmsg)
self.recvtrs.start()
self.hearttrs=threading.Thread(target=self.heartbeat)
self.hearttrs.start()
defsendmsg(self):
whileTrue:
data=raw_input(">:")
ifnotdata:
break
self.clientsock.sendto(data.encode('utf-8'),self.ADDR)
ifdata.upper()=='QUIT':
self.clientsock.close()
break
defheartbeat(self):
whileTrue:
self.clientsock.sendto('HEARTBEAT',self.ADDR)
time.sleep(BEAT_PERIOD)
defrecvmsg(self):
whileTrue:
recvData,addr=self.clientsock.recvfrom(1024)
ifnotrecvData:
break
print(u'從%s收到信息:%s'%(self.HOST,recvData.decode('utf8')))if__name__=="__main__":
udpClient=UdpClient()
udpClient.sendmsg()