Created
August 4, 2016 12:32
-
-
Save adventureloop/9bba49b214768ed36717060246d18916 to your computer and use it in GitHub Desktop.
micropython socket example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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
How to use sock.recv?Please give me a code example.Please......