Test on Python network programming-socket

Source: Internet
Author: User

Because the project requires a lot of data, even though databases released by some research centers on the Internet are charged, it is a luxury for me to have three employees.

(A human face recognition database requires 1000 million USD! WTF !)

 


Try to develop a search tool by yourself...

 


First test a small socket function (very small, the experts will pass ...)


[Python] # testint of sending infomation to www.2cto.com
 
Import sys, socket, time
 
Host = sys. argv [1]
Xport = sys. argv [2]
Filename = sys. argv [3]
 
Print ('getting socket ...')
 
Try:
S = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
Failed t socket. error, e:
Print ('here is a error: % s') % e
Sys. exit (1)
 
Print ('done ')
Print ('getting socket port ')
 
Try:
Port = int (xport)
Failed t ValueError:
Try:
Port = socket. getprotobyname (xport, 'tcp ')
Failed t socket. error, e:
Print ('here is an error happened: % s') % e
Sys. exit (1)
 
Print ('done ')
Print ('ing ing the host ')
 
Try:
S. connect (host, port ))
Handle t socket. gaierror, e:
Print ('here is an error happened: % s') % e
Sys. exit (1)
Failed t socket. error, e:
Print ('here is an error happened: % s') % e
Sys. exit (1)
 
Print ('done ')
 
Print ('sleeping ...')
Time. sleep (3)
Print ('ing ing ...')
 
Try:
S. sendall ('get % s HTTP/1.0 \ r \ n \ r \ n' % filename)
 
Failed t socket. error, e:
Print ('here is an error happened: % s') % e
Sys. exit (1)
 
Try:
S. shutdown (1)
Failed t socket. error, e:
Print ('here is an error happened: % s') % e
Sys. exit (1)
 
While True:
Try:
Buf = s. recv (64)
Failed t socket. error, e:
Print ('here is an error happened: % s') % e
Sys. exit (1)
 
If not len (buf ):
Break
Sys. stdout. write (buf)
# Testint of sending infomation to www.2cto.com

Import sys, socket, time

Host = sys. argv [1]
Xport = sys. argv [2]
Filename = sys. argv [3]

Print ('getting socket ...')

Try:
S = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
Failed t socket. error, e:
Print ('here is a error: % s') % e
Sys. exit (1)

Print ('done ')
Print ('getting socket port ')

Try:
Port = int (xport)
Failed t ValueError:
Try:
Port = socket. getprotobyname (xport, 'tcp ')
Failed t socket. error, e:
Print ('here is an error happened: % s') % e
Sys. exit (1)

Print ('done ')
Print ('ing ing the host ')

Try:
S. connect (host, port ))
Handle t socket. gaierror, e:
Print ('here is an error happened: % s') % e
Sys. exit (1)
Failed t socket. error, e:
Print ('here is an error happened: % s') % e
Sys. exit (1)

Print ('done ')

Print ('sleeping ...')
Time. sleep (3)
Print ('ing ing ...')

Try:
S. sendall ('get % s HTTP/1.0 \ r \ n \ r \ n' % filename)

Failed t socket. error, e:
Print ('here is an error happened: % s') % e
Sys. exit (1)

Try:
S. shutdown (1)
Failed t socket. error, e:
Print ('here is an error happened: % s') % e
Sys. exit (1)

While True:
Try:
Buf = s. recv (64)
Failed t socket. error, e:
Print ('here is an error happened: % s') % e
Sys. exit (1)

If not len (buf ):
Break
Sys. stdout. write (buf)

 

 

The socket module of Python (version 2.6 and version 3.2 have not been tried yet, and it is estimated that it is similar) defines four possible errors:

1 related to general IO and communication problems: socket. error

2. query address information: socket. gaierror

3 related to other address errors: socket. herror

4. It is related to the processing timeout after settimeout () is called on a socket: socket. timeout

 


The above code has a major problem, that is, the socket is not closed, because the shutdown function of the socket module needs to be tested (because the shutdown function may cause some problems if the program is improperly designed ), add this code segment.


[Python] try:
S. shutdown (1)
Failed t socket. error, e:
Print ('here is an error: % s') % e
Sys. exit (1)
Try:
S. shutdown (1)
Failed t socket. error, e:
Print ('here is an error: % s') % e
Sys. exit (1)

Lol

 

 

From the column of FishinLab

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.