Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- #create socket - parameters tell type of socket (IP), and protocol (TCP)
- server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- #bind socket to address - all addresses on this computer, on port 8081
- #8081 typically used for testing
- server_socket.bind(("0.0.0.0", 8081))
- #set socket to listen
- server_socket.listen()
- print("waiting for connection")
- #accept socket - waits, accepts w/ two return
- connection_socket, address = server_socket.accept()
- print("client connected")
- #added in section 1.8
- #sends data
- message = "Hello, thanks for connecting"
- data = message.encode() #encodes message as a series of bytes
- connection_socket.send(data)
- #challenge at the end of 1.8
- #receive and display a response message
- data = connection_socket.recv(1024)
- response = data.decode()
- print(response)
- #close sockets
- connection_socket.close()
- server_socket.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement