Python socket module

Source: Internet
Author: User

1. Socket definition: Software module to provide TCP/IP communication for upper layer software

Socket module workflow, server-side start socket,accept, that is, to create a connection instance itself to maintain the connection status, get a client connection instance, waiting for the connection. The client calls the socket and connects to connect to create a connection instance.

2, in the local simulation client connection process, see the following example.

First Encounter common errors

ImportSocketserversock=Socket.socket ()#Serversock.bind ((' 192.168.148.212 ', 9999,)) #注意这里的端口需要intServersock.bind (('127.0.0.0', 9999,)) Serversock.listen (3)#start the listening queueConinstance,ipadd =serversock.accept ()Print(Ipadd,'o---o', Coninstance)>>>Restart:c:/users/administrator/appdata/local/programs/python/python35-32/sersock.py Traceback (most recent): File"c:/users/administrator/appdata/local/programs/python/python35-32/sersock.py", Line 7,inch<module>Serversock.bind (('127.0.0.0', 9999,)) OSError: [Winerror10049] In its context, the address of the request is invalid.

-*-* OSError: [Winerror 10049] In its context, the address of the request is invalid.

Actually because the IP address is not written correctly, then how to view the IP and open port?
A) Ipconfig/all view native IP address

b) Netstat-an view open IP ports

If you use 127.0.0.1 port 5037 you may see such an error

-*-* OSError: [Winerror 10048] typically each socket address (Protocol/network address/port) is allowed only once.

This may be due to the fact that the port is already occupied, replacing an infrequently used port,9999 and no longer prompting for such errors.

-*-* connectionrefusederror: [Winerror 10061] Unable to connect because the target computer was actively rejected.

When you use FTP test, close FTP using the command quit. As soon as you open FTP using Task Manager or directly close cmd, it will not take effect.

A) Check that window's server is turned on. Open to turn it on. Right-click My Computer-management-services-server

b) Check if the Telnet service of window is turned on (socket module is based on TCP protocol), does not turn it on, Control Panel-Program-turn window function on or off-telnet client and server

Problem root due to unclear circumvention method using python27 problem software python 3.5.1

Python's 2.7 Interpreter may also have problems with the client being executed only once, and when the second execution is reported 10061. [Winerror 10061] Unable to connect because the target computer was actively rejected.

To see the effects of client and server connections, you can execute both the server code and the client code in two cmd command windows, respectively.

Server code

Import= socket.socket () server.bind('127.0.0.1', 9000) Server.listen (5)print ('wait ... ' ) while True:       conn,address= server.accept ()    Print (Address,'o---o', conn)

Client code

Import Socket Import  =print  (CLI) cli.connect ('127.0.0.1', 9000)  Print ('2') time.sleep (1)# cli.close ()

3, analog network chat times wrong, troubleshooting: 1 To troubleshoot the cycle, normal 2 will Sendall () in the String plus str () processing, still show errors.

Note: The recv and the server accept are waiting for input, that is, if there is no input to the end, will wait. And when the recv is in the loop, the break loop, recv to receive input, the loop terminates, then recv will error.

Socket.error: [Errno 10053]

Basic OK is on server side, error at SERVER.RECV (1024)

-*-*[errno 10057] Because the socket is not connected and (when using a sendto call to send the number

Server Side

ImportSocketImportSYSImportTimeserver=socket.socket () Server.bind ('127.0.0.1', 9000)) Server.listen (5)Print('wait ...') whiletrue:conint,address=server.accept () Conint.sendall ('Hello,welcome')    Print(Address,'o---o', Conint) whileTrue:#serrecv = server.recv (1024x768)        " "if Serrecv = = ' Remote Shutdown ': sys.exit (0) else:server.sendall (serrecv+ ' Go Od ')" "Time.sleep (2) Server.sendall (str (SERVER.RECV (1024) +'Good'))

Client Side

ImportSocketImporttimecli=Socket.socket ()Print(CLI) cli.connect (('127.0.0.1', 9000))Print('2') Sercon= CLI.RECV (1024)PrintSercon whileTrue:time.sleep (1) INP= Raw_input ('Pleaes Input:')    ifInP! ='quit': Cli.sendall (str (INP))PrintCLI.RECV (1024)    Else:         Breakcli.close ()

Cause of Error: Server.sendall (str (SERVER.RECV (1024x768) + ' good '))

This should be done using server.accept () with the client connection instance as the object, i.e. Conint, to CONINT.RECV ().

Python socket module

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.