小编Kre*_*air的帖子

Python HTTP HEAD - 正确处理重定向?

我可以使用urllib2来生成HEAD请求,如下所示:

import urllib2
request = urllib2.Request('http://example.com')
request.get_method = lambda: 'HEAD'
urllib2.urlopen(request)
Run Code Online (Sandbox Code Playgroud)

问题是,当重定向后,它会使用GET而不是HEAD.

此HEAD请求的目的是检查我即将下载的URL的大小和内容类型,以便我可以确保不下载一些大文档.(URL由随机互联网用户通过IRC提供).

如何在重定向后使用HEAD请求?

python redirect urllib2 head

8
推荐指数
2
解决办法
5000
查看次数

使用python urllib2在http标头中传递会话cookie?

我正在尝试使用Mediawiki api编写一个简单的脚本来登录Wikipedia并在我的用户页面上执行一些操作.但是,我似乎永远不会超过第一个登录请求(来自此页面:https://en.wikipedia.org/wiki/Wikipedia : Creating_a_bot#Logging_in).我不认为我设置的会话cookie正在发送.到目前为止这是我的代码:

import Cookie, urllib, urllib2, xml.etree.ElementTree

url = 'https://en.wikipedia.org/w/api.php?action=login&format=xml'
username = 'user'
password = 'password'

user_data = [('lgname', username), ('lgpassword', password)]

#Login step 1
#Make the POST request
request = urllib2.Request(url)
data = urllib.urlencode(user_data)
login_raw_data1 = urllib2.urlopen(request, data).read()

#Parse the XML for the login information
login_data1 = xml.etree.ElementTree.fromstring(login_raw_data1)
login_tag = login_data1.find('login')
token = login_tag.attrib['token']
cookieprefix = login_tag.attrib['cookieprefix']
sessionid = login_tag.attrib['sessionid']

#Set the cookies
cookie = Cookie.SimpleCookie()
cookie[cookieprefix + '_session'] = sessionid

#Login step …
Run Code Online (Sandbox Code Playgroud)

python cookies http wikipedia-api

7
推荐指数
1
解决办法
8792
查看次数

这个字符串是Base64吗?我怎么知道使用的编码是什么?

这对我来说是一个难题,我真的很生气,我无法解决它!所以,如果有人有空闲时间,我想在这里提出一些如何解决它的建议!

我使用的软件将密码存储在oracle数据库中.密码字段的类型为Varchar2(100个字符).在我看来,该软件对密码进行编码并将编码的字符串存储在数据库中.

我的密码是'1234',编码的字符串是'cRDtpNCeBiql5KOQsKVyrA0sAiA ='.数据库中的所有密码长度为28个字符.

我分配给自己的难题是找到字符串的编码和/或加密.我的第一张支票是在Base64上

所以这是我在python中的第一个测试(空闲):

>>> import base64
>>> encoded = 'cRDtpNCeBiql5KOQsKVyrA0sAiA='
>>> decoded = base64.b64decode(encoded)
>>> decoded
'q\x10\xed\xa4\xd0\x9e\x06*\xa5\xe4\xa3\x90\xb0\xa5r\xac\r,\x02 '
>>> print decoded
qí??*????r?
Run Code Online (Sandbox Code Playgroud)

,

这是我的第二次测试:

>>> myString = '1234'
>>> encoded = base64.b64encode(myString)
>>> encoded
'MTIzNA=='
>>> decoded = base64.b64decode('MTIzNA==')
>>> decoded
'1234'
Run Code Online (Sandbox Code Playgroud)

所以,我首先想到的是这不是Base64编码的.在我检查维基百科(https://en.wikipedia.org/wiki/Base64)之后,似乎Base64编码的字符串不是固定大小的.我的第二个想法是字符串被加密然后编码到Base64,这就是为什么我得到奇怪的解码字符串.

有任何想法吗?

python oracle encryption base64 encoding

6
推荐指数
2
解决办法
7940
查看次数

插入在C#中的字符串数组上排序

如果我有一个字符串数组,例如

string[] names = {"John Doe", "Doe John", "Another Name", "Name Another"};
Run Code Online (Sandbox Code Playgroud)

如何使用插入排序对此数组进行排序?

维基百科有一些例子:https://en.wikibooks.org/wiki/Algorithm_implementation/Sorting/Insertion_sort#C.23

static void InsertSort(IComparable[] array)
{
    int i, j;

    for (i = 1; i < array.Length; i++)
    {
        IComparable value = array[i];
        j = i - 1;
        while ((j >= 0) && (array[j].CompareTo(value) > 0))
        {
            array[j + 1] = array[j];
            j--;
        }
        array[j + 1] = value;
    }
}
Run Code Online (Sandbox Code Playgroud)

static void InsertSort<T>(IList<T> list) where T : IComparable<T>
{
    int i, j;

    for …
Run Code Online (Sandbox Code Playgroud)

c# sorting algorithm insertion-sort

3
推荐指数
1
解决办法
2万
查看次数