Build a small virtual LAN communication program using Python 3.6 and Vmware WorkStation

Source: Internet
Author: User

1. Test environment:
Host system: WIN10, Python 3.5
Virtual machine system: Win7 64-bit, Python 3.6, Vmware WorkStation 12

2, in the "Virtual network Editor" Do not need to make any changes, neither need to check the "bridge mode" also does not need to check "NAT mode", do not need to set "DHCP", you can implement host and virtual machine local area network formation;

3, the host as the server, the code is as follows:

Import= socket.socket () s.bind('192.168.136.1', 1234) S.listen (5) while True:    = s.accept ()    c.send (b' Thank for connecting ' )    c.close ()

Attention:
1) s.accept () returns a tuple (c, addr), C is a client socket, addr is an address, uses C to send data and closes the socket;
2) use prefix B in c.send to convert a string to byte type

4, the virtual machine as a client, the code is as follows:

Import= socket.socket () s.connect('192.168.136.1', 1234)  Print(S.RECV (1024))

After executing the client code, get the string B ' Thank for connecting '

Attached: The socket principle in Python. Most network programming in Python hides the basic details of the socket module, not directly interacting with sockets. Sockets consist of two: server and client. After you create a service-side socket, let it wait for the connection. This allows it to listen at a network address (a combination of IP addresses and a port number) until the client socket is connected. Once the connection is complete, the two can interact.
Processing a client socket is usually easier than processing a server-side socket, because the service side must be ready to handle the client's connection at any time, while processing multiple connections, while the client simply connects, completes the transaction, disconnects. (For simplicity, use the Socketserver class family and the Twisted framework to handle server-side programming)
A socket is an instance of the socket class in the socket module. Its instantiation requires 3 parameters: the 1th parameter is the address family (default is Socket.af_inet), and the 2nd parameter is the stream (socket. Sock_stream, default value) or datagram (socket. SOCK_DGRAM) socket; The 3rd parameter is the protocol used (default is 0, which is the default value).You do not need to provide any parameters for a common socket.
After the service-side socket uses the Bind method, the Listen method is called to listen for a specific address. The client socket uses the Connect method to connect to the server, and the address used in the Connect method is the same as the address of the server in the Bind method (on the server side, many functions can be implemented, such as using the function Socket.gethostname to get the current hostname). In this case, an address is a tuple in the format (host, port) where host is the hostname (such as ' 192.168.136.1 ') and port is the port number (an integer). The Listen method is a parameter, which is the length of the connection that the service side has not processed (that is, the number of connections that are allowed to wait until they are disabled).
After the service-side socket starts listening, it can accept the client connection. This step is done using the Accept method. This method blocks (waits) until the client connects, and then returns a tuple in the format (client, address), which is a client socket, and address is one. After the server finishes processing the connection to the client, call the Accept method again to start waiting for the next connection. This process is usually implemented in an infinite loop.
Sockets have two methods: Send and recv, which are used to transmit and receive data. You can use a string parameter to call send to send data and call recv with the maximum number of bytes required to receive data. If you are unsure of which number to use, then 1024 is a good choice.

Build a small virtual LAN communication program using Python 3.6 and Vmware WorkStation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.