TCP/IP socket Networking
클라이언트는 서버측에게 message 를 보낸다.
서버에서는 클라이언트로부터 받은 Message 를 받아서 판별하고 해당값따른 다른값을 Send 해준다.
클라이언트는 서버로부터 받은 메세지를 print 해준다.
Echo 서버, Echo 클라이언트를 약간수정해보았다.
Client Side Code.
# Python 2.7 Version Client
# OS Raspbian < Raspberry pi 3>
import socket
import sys
import select
from time import ctime
HOST = '192.168.0.90'
PORT = 9999
ADDR = (HOST,PORT)
clinetSocket = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
try:
Msg = 'hi' # Message Variable
clientSocket.connect(ADDR) # connect Server
clientSocket.send(Msg) # Send Message
except Exception as e:
print('%s %s' % ADDR)
sys.exit()
receiveMsg = clientSocket.recv(1024)
print('RECV : %s' % receiveMsg )
Server Side Code.
# Python 2.7 Version Server Side
# OS windows 10 < PC >
import socket
import select
HOST = ''
PORT = 9999
BUFSIZE = 1024
ADDR = (HOST,PORT)
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSocket.bind(ADDR)
print('bind')
serverSocket.listen(10)
print('listen')
clientSocket, addr_info = serverSocket.accept()
print('accept')
print('---client Info----')
print(clientSocket)
data = clientSocket.recv(10000)
if data == 'hi':
clientSocket.send(data) #receive 값을 판별하여 해당값이맞을경우 client 측에보냄
else:
print("not")
clientSocket.close()
serverSocket.close()
print('closed')
누구나 파이썬 -3 (연산자, if, for) (0) | 2019.12.22 |
---|---|
누구나 파이썬 -2 (문법 , 자료형) (0) | 2019.12.13 |
누구나 파이썬 -1 (파이썬 설치하기 2.7.1 version) (0) | 2019.12.12 |
Python HTTP Networking request (308) | 2018.12.18 |
how to use File I/O in Python (0) | 2018.12.18 |
Python 환경변수 지정법. (0) | 2018.11.20 |