HTTP basic authentication using sockets in python

后端 未结 2 1168
萌比男神i
萌比男神i 2021-01-01 02:09

How to connect to a server using basic http auth thru sockets in python .I don\'t want to use urllib/urllib2 etc as my program does some low level socket I/O operations

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-01 02:42

    Probably the easiest place to start is using makefile() to get a simpler file-like interface to the socket.

    import socket, base64
    
    host= 'www.example.com'
    path= '/'
    username= 'fred'
    password= 'bloggs'
    token= base64.encodestring('%s:%s' % (username, password)).strip()
    
    lines= [
        'GET %s HTTP/1.1' % path,
        'Host: %s' % host,
        'Authorization: Basic %s' % token,
        'Connection: close',
    ]
    
    s= socket.socket()
    s.connect((host, 80))
    f= s.makefile('rwb', bufsize=0)
    f.write('\r\n'.join(lines)+'\r\n\r\n')
    response= f.read()
    f.close()
    s.close()
    

    You'll have to do a lot more work than that if you need to interpret the returned response to pick out the HTML or auth-required headers, and handle redirects, errors, transfer-encoding and all that right. HTTP can be complex! Are you sure you need to use a low-level socket?

提交回复
热议问题