python socket GET

前端 未结 6 1418
醉酒成梦
醉酒成梦 2020-12-09 20:20

From the other posts on stack overflow this should be working

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)                 

s.connec         


        
6条回答
  •  萌比男神i
    2020-12-09 20:49

    I am cleaning up the examples for Python 3. We need bytes/string conversion and we can also use automatic closing of the connection using with:

    #!/usr/bin/env python3
    
    import socket
    
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    
        s.connect(("example.com" , 80))
        s.sendall(b"GET / HTTP/1.1\r\nHost: example.com\r\nAccept: text/html\r\n\r\n")
        print(str(s.recv(4096), 'utf-8'))
    

提交回复
热议问题