Python Network programming

Source: Internet
Author: User

1. Specific connection process I'm not going to say it. See also: http://woodpecker.org.cn/

2. Use the server-side and client-side communication examples to illustrate

3. On the Code

Service side (server.py)

1 #-*-coding:utf-8-*-2 ImportSocket3host="127.0.0.1"4port= 99995s =Socket.socket (Socket.af_inet,socket. SOCK_STREAM)6 S.bind ((host,port))7S.listen (1)8  while1:9Conn,addr=s.accept ()Tendata = CONN.RECV (1024) One     ifdata = ='Bye': A          Break -     Else: -         PrintData theTosend = Raw_input (">") - conn.send (tosend) -     ifTosend = ='Bye': -          Break +S.close ()
View Code

Client (client.py)

1 #-*-coding:utf-8-*-2 ImportSocket3s =Socket.socket (Socket.af_inet,socket. SOCK_STREAM)4Host ='127.0.0.1'  5Port = 99996 S.connect ((host,port))7  while1:8Tosend = Raw_input (">")9 s.send (tosend)Ten     ifTosend = ='Bye': One          Break Adata = S.RECV (1024) -     ifdata = ='Bye': -          Break the     Else: -         PrintData -S.close ()
View Code

4. When you run the test, first open the server side, and then you understand.

5.

#编写过程中遇到的问题在端口 (PORT) and hostname (host) are bound to note that the parentheses are two levels, or error, the host's single or double quotation marks do not matter

#端口要对应

Python Network programming

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.