Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- DART
- TEST
- Dialog
- 계측
- viewmodel
- CustomScrollView
- 안드로이드
- android
- drift
- data
- binding
- ScrollView
- Coroutines
- activity
- 테스트
- 앱바
- scroll
- textfield
- Compose
- intent
- textview
- appbar
- Navigation
- tabbar
- LifeCycle
- Flutter
- Kotlin
- 앱
- livedata
- Button
Archives
- Today
- Total
Study Record
[파이썬] 소켓 프로그래밍 본문
728x90
소켓 - Server
import socket
import sys
host = '127.0.0.1'
port = 4444
try:
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen(1)
client, address = server.accept()
except Exception as e:
sys.exit('Error:', e)
else:
print("Connected by", address)
recv_msg = b''
while True:
data = client.recv(1024)
if not data:
client.close()
break
recv_msg += data
print(len(recv_msg), recv_msg)
finally:
server.close()
소켓 - client
import socket
import sys
host = '127.0.0.1'
port = 4444
try:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((host, port))
except Exception as e:
sys.exit("Error: ", e)
else:
client.sendall(open('plain.txt', 'rb').read())
finally:
client.close()
소켓 에코 프로그램 - server
클라이언트에서 단순하게 메세지를 서버로 전송하고 그 메시지를 다시 클라이언트로 보내주는 프로그램이다.
# Echo server program
import socket
def main():
HOST = '' # 사용 가능한 모든 인터페이스 주소
PORT = 4444 # 임의의 포트
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
if __name__ == '__main__':
main()
소켓 에코 프로그램 - client
import socket
def main():
HOST = '127.0.0.1'
PORT = 4444
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
if __name__ == '__main__':
main()
728x90
'서버보안 > 리눅스 서버보안' 카테고리의 다른 글
[파이썬] 패키지와 모듈 (0) | 2021.11.29 |
---|---|
[파이썬] 클래스와 인스턴스와 모듈 (0) | 2021.11.29 |
[파이썬] 연산자 (0) | 2021.11.24 |
[파이썬] 기본 문법(조건문, 반복문, 함수) (0) | 2021.11.24 |
[파이썬] 집합(Set) (0) | 2021.11.24 |