このページは個人的にPython勉強のメモです。今回はNetwork Connectionsについて話ししたいと思います。まずは覚えて置かないといけないの単語が幾つありますね。
Communication end point−Socketsのこと。
AF_INET−IPV4のこと(Address Family internet)
IP AddressとPort−例えの話になると、IP Addressはあなたが住んでるデパートでPortはあなたが住んでる部屋番号です。
2種類の通信方法があります。
Connection-oriented
電話通信みたいにちゃんと接続確認してからコミュニケーションする。
TCP[SOCK_STREAM]
Connectionless
接続確認なしで。
Server
あるもの(Hardware/Software)であるサービスを”Client”へ提供するってこと。Clientのリクエスト待ちと返信する。
Client
Serverへリクエストを送ってデータとかもらうっものです。
Hardwareの例え…
PrinterがはServerでパソコンはClientでFile Print!というリクエストが送る、Printerはこれを答えてPrintしたりします。
Softwareの例え…
WebServerで色々な人がパソコンを通じてWEB、WEB Applicationをアクセスします。Serverはこれに答えてWEBの情報などなど返しします。
Code Example In TCP
Server Side-
#Create a TCP Server #Lower-level networking interface #Using socket from socket import* from time import ctime #Create a socket object from the given address family, #socket type, and protocol type (optional) #an indication to the build() methond that can be using any addresss #a random port that the system is not using #the data size host='' port=21567 buff=1024 add=(host,port) tcpServerSocket=socket(AF_INET,SOCK_STREAM) #socket moudle method for create a TCP/IP connections tcpServerSocket.bind(add) #Bind address(host,port) tcpServerSocket.listen(5) #Setup the TCP listener while True: #infinite loop,waiting connection #the first loop, print ('waiting for connection....') tcpCliSock,add = tcpServerSocket.accept() #accept TCP clint connections print ('Connected from...',add) while True: data=tcpCliSock.recv(buff) if not data:#break loop while blank is sent break Message=('Received!'+ctime()) print('Data Received: ',data.decode('utf-8')) print(ctime()) MessageInBytes=bytes(Message,'utf-8') tcpCliSock.send(MessageInBytes) tcpCliSock.close() tcpServerSocket.close() print('The connection is closed.')
Client Side-
from socket import * host='localhost' port=21567 buff=1024 Addr=(host,port) tcpCliSock=socket(AF_INET,SOCK_STREAM) tcpCliSock.connect(Addr) while True: data = input('> ') if not data: break dataInBytes=bytes(data,'utf-8') tcpCliSock.send(dataInBytes) AnsInBytes=tcpCliSock.recv(buff) if not data: break print(AnsInBytes.decode('utf-8')) tcpCliSock.close()
Result:
[php]Server
waiting for connection….[/php]
[php]Client> HELLO[/php]
[php]Server
Connected from… (‘127.0.0.1’, 61439)
Data Received: HELLO
Sun Dec 25 09:02:19 2016[/php]
[php]Client
Received!Sun Dec 25 09:02:19 2016[/php]
Code Example UDT
Server Side-
#Server Side in UDP from socket import * from time import ctime host='' port=3000 buff=1024 addr=(host,port) udtServer=socket(AF_INET,SOCK_DGRAM) udtServer.bind(addr) try: while True: print("Waiting the message...") data,addr=udtServer.recvfrom(buff) Message = ('Received!' + ctime()) MessageInBytes = bytes(Message, 'utf-8') udtServer.sendto(MessageInBytes,addr) print ('Data is send to...',addr) udtServer.close() except(KeyboardInterrupt,EOFError): print('Servies is out..') udtServer.close()
Client Side-
#UDP Client from socket import * host='localhost' port=3000 Buff=1024 addr=(host,port) updClient=socket(AF_INET,SOCK_DGRAM) while True: try: data=input('> ') if not data: break dataInBytes = bytes(data, 'utf-8') updClient.sendto(dataInBytes,addr) dataInBytes,addr=updClient.recvfrom(Buff) if not dataInBytes: break print (dataInBytes) except (KeyboardInterrupt,EOFError): print("out of services") break updClient.close()
Result
[php]Server
Waiting the message…[/php]
[php]Client
> HELLO[/php]
[php]Server
Data is send to… (‘127.0.0.1’, 52293)
Waiting the message..[/php]
[php]Client
b’Received!Sun Dec 25 09:04:57 2016′[/php]
socketserver code example
Server side-
#UDP Client from socket import * host='localhost' port=3000 Buff=1024 addr=(host,port) updClient=socket(AF_INET,SOCK_DGRAM) while True: try: data=input('> ') if not data: break dataInBytes = bytes(data, 'utf-8') updClient.sendto(dataInBytes,addr) dataInBytes,addr=updClient.recvfrom(Buff) if not dataInBytes: break print (dataInBytes) except (KeyboardInterrupt,EOFError): print("out of services") break updClient.close()
Client Side
#Socketserver TCP Client from socket import * #Basic client code host='localhost' port=3000 buff=1024 addr=(host,port) while True: TcpClient=socket(AF_INET,SOCK_STREAM) TcpClient.connect(addr) data=input('> ') if not data: break data=data+'\n' print (data) dataInBytes = bytes(data,'utf-8') TcpClient.send(dataInBytes) data=TcpClient.recv(buff) if not data: break print (data) TcpClient.close()
Result
[php]Server:
waiting …[/php]
[php]Client:
HELLO[/php]
[php]Server:
waiting …
Connect from.. (‘127.0.0.1’, 61454)
get
b’HELLO\n'[/php]
[php]Client:
b’Received! :)Sun Dec 25 09:10:14 2016′[/php]