Socket first attempt in Python

Source: Internet
Author: User


Forget, chose Python on the way down, pipe right or wrong, since the choice of the front, do not care trials and hardships, today saw the Python socket programming, the real encountered a variety of problems, firewalls, error 10054, 10049, their own on Baidu on various search, Finally came out, the bug out of the feeling that only the most clear. These are the sockets of their own time, some notes:

1. Netstat-no This command is to look at the port, and you can see through the PID which programs are using these ports!

2 in Python or in other languages, using sockets to write server and client communication programs often encounters an error:
Socket.error: [Errno 10048] typically each socket address (Protocol/network address/port) is allowed only once this prompt appears when port conflicts occur, possibly because after creating a socket in the server program opens a port, This socket is not closed at the end of the program, so the next time you start the program you will get this error, workaround: 1, close the socket 2 at the end of the server program, or replace the port number 3, restart the machine

3 ipconfig View the IP of this machine

Client

#clientimport sockethost = ' 10.11.29.186 ' PORT = 8156s = Socket.socket (socket.af_inet,socket. Sock_stream) S.connect ((Host,port)) S.sendall ("Hello world") data = S.recv (1024x768) s.close () print ' Received ', repr (data)

Sever

# sever Programimport sockethost = ' PORT = 8156s = Socket.socket (socket.af_inet,socket. Sock_stream) S.bind ((Host,port)) S.listen (1) conn,addr = s.accept () print ' Connected by ', Addrwhile 1:    data = CONN.RECV (1024x768)    if not data:break    conn.sendall (data)    Conn.close ()


The result of the final operation:

Source:

Socket first attempt in Python

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.