Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adventureloop/9bba49b214768ed36717060246d18916 to your computer and use it in GitHub Desktop.
Save adventureloop/9bba49b214768ed36717060246d18916 to your computer and use it in GitHub Desktop.
micropython socket example
import usocket as socket
#import socket
def sendudp(host, port):
address = ("127.0.0.1", 6969)
data = b'hello udp'
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print("data is type {}".format(type(data)))
sock.sendto(data, address)
def sendtcp(host, port):
data = b'hello tcp'
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("data is type {}".format(type(data)))
sock.connect((host, port))
sock.send(data)
sock.close()
if __name__ == "__main__":
sendtcp("127.0.0.1", 6969)
sendudp("127.0.0.1", 6969)
@chenqilongwuxi
Copy link

How to use sock.recv?Please give me a code example.Please......

@coolwolf
Copy link

@chenqilongwuxi this is the tcp and udp client example. not the server. you should look for socket or tcp server example for micropython.

This may help you [https://github.com/micropython/micropython/blob/master/examples/network/http_server_simplistic_commented.py]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment