python socketのメモ

'''
Memo for network programming by python
'''
import socket
host = "foo.bar.com"
port = 12345
msg = "Hello"
buffersize = 4096
## Create TCP socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
## Connect to the host
client.connect((host, port))
## Send some data
client.send(msg)
## Recieve some data
response = client.recv(buffersize)
## Create UDP socket
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
## UDP is a connectionless protocol so no need to call connect() beforehand
## Send some data
client.sendto(msg, (host, port))
## Recieve some data
recvdata, addr = client.recvfrom(buffersize) #recvfrom returns both data and the detail of the remote host and port, AKA address

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.